1845943 Members
4966 Online
110250 Solutions
New Discussion

Re: AWK again...

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

AWK again...

Hi,
I have the text file in the format below:
#cat file1.txt
23400 joe_bloggs
56690 sunny_lee
56789 terrence_lam

The left most column represent the disk-usage by the the users on the right column, i.e. their home directory disk usage.
I would like to display only users >40MB i.e 40000k of space into a different file. I did the following:
#!/bin/sh
for i in `cat file1`
do
if($1 > 40000)
echo $1 $2 >> list.txt
done

It produced the following error:
Syntax error at line 3: ')' is not expected

I had also tried the one below on the console:
#cat file1 | awk `{if($1>40000)}{print $1 $2}`

and it produced the same kind of error.

Can someone help me out in this?
Thanks.


8 REPLIES 8
Tom Geudens
Honored Contributor
Solution

Re: AWK again...

Hi,
Something like
cat file1 | awk '{ if ($1 > 40000) print $2 }'
might do the trick

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
John Carr_2
Honored Contributor

Re: AWK again...

Hi

you may wish to sort this into ascending order too.

cat file1 | awk '{ if ($1 > 40000) print $2 }' | sort +n +0

cheers
John.
Robin Wakefield
Honored Contributor

Re: AWK again...

Don't forget you'll need to print out both fields for the sort:

awk '$1>40000{ print }' filename | sort +0n

Rgds, Robin.
Chern Jian Leaw
Regular Advisor

Re: AWK again...

Robin,

I tried the following which you suggested:
awk '$1>40000{ print }' filename | sort +0n

Unfortunately, it produced as error:
Error context is
$1 >>>> 40000{print}' <<<awk: Quitting The source is at line 1

I'm not sure if it I did the right way...

Could you clarify?

Thanks

John Carr_2
Honored Contributor

Re: AWK again...

Hi

an over sight on my part this should have been

cat file1 | awk '{ if ($1 > 40000) print $1 " " $2 }' | sort +n +0


John
Tom Geudens
Honored Contributor

Re: AWK again...

Hi,
I think you forgot the blanks inside the { } :
awk '$1>40000{ print }' file1 | sort +0n
You also need to replace "filename" by your filename, "file1".

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
John Carr_2
Honored Contributor

Re: AWK again...

Hi

Tom's answer is the right one and best one

John.
Chern Jian Leaw
Regular Advisor

Re: AWK again...

All,
thanks very much - solutions were simple yet helpful.

Tom,
yes, I forgot the space between { print }!!

Thanks all