awk command doubt

Go down

awk command doubt

Post by anand on Thu Mar 04, 2010 6:40 pm

What is awk?
NF?
FS?
OFS?
$0?
-F?
' '?
avatar
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: awk command doubt

Post by Maithreyi on Thu Mar 04, 2010 6:43 pm

NF - number of fields
FS - Field Separator
$0 - refers to the complete line , whereas $1 refers to field 1

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: awk command doubt

Post by anand on Thu Mar 04, 2010 6:48 pm

how to use awk to locate lines longer than 100 and smaller than 150 characters.......?
avatar
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: awk command doubt

Post by Maithreyi on Thu Mar 04, 2010 7:01 pm

awk ' { if (length > 100 && length < 150) print }' filename

I have edited this now.Must be spacing problem.It is working for me!


Last edited by Maithreyi on Thu Mar 04, 2010 7:09 pm; edited 1 time in total (Reason for editing : Spacing error)

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: awk command doubt

Post by anand on Thu Mar 04, 2010 7:08 pm

[335723@oracleclient ~]$ awk '{length 5 && length < 10 ) print }' aaa.txt
awk: {length 5 && length < 10 ) print }
awk: ^ syntax error

help me out...........
avatar
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: awk command doubt

Post by Maithreyi on Thu Mar 04, 2010 7:11 pm

Where is the if clause ? And inside if the condition is enclosed in ()

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: awk command doubt

Post by Saradha Kannan on Tue Mar 16, 2010 1:31 pm

What is the use of BEGIN pattern in awk cmd?
Can any1 explain with an example??

Saradha Kannan

Posts : 25
Points : 25
Join date : 2010-03-04

View user profile

Back to top Go down

Re: awk command doubt

Post by Christopher on Tue Mar 16, 2010 1:49 pm

BEGIN block ll be executed once the execution of awk file starts.
END block ll be executed once the exection ends.
Other blocks ll be executed for all ur inputs.

Example

Code:
$ cat sample.awk
BEGIN { print "WELCOME TO UNIX" }
{ print $1 }
END { print "THANK U.. VISIT AGAIN... " }

$ awk -f sample.awk
WELCOME TO UNIX
orange colour
orange
candle light
candle
THANK U.. VISIT AGAIN...
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: awk command doubt

Post by Saradha Kannan on Tue Mar 16, 2010 2:20 pm

Hey thanks.. Smile
understood

Saradha Kannan

Posts : 25
Points : 25
Join date : 2010-03-04

View user profile

Back to top Go down

Re: awk command doubt

Post by jennyinhere on Wed Mar 24, 2010 12:41 pm

Can someone post more examples of awk command usage with explanation?

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: awk command doubt

Post by Maithreyi on Wed Mar 24, 2010 1:23 pm

awk basically reads one line at a time, matches the particular pattern specified.If the match is found, it performs the action on that line!

Eg :- awk '/gold/ {print}' coins.txt

If gold is found in any line of the file 'coins.txt' , then print all lines in coins.txt containing gold.

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: awk command doubt

Post by jennyinhere on Wed Mar 24, 2010 2:28 pm

What does awk -f sample.awk do ?

What is the purpose of -f ?

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: awk command doubt

Post by Christopher on Thu Mar 25, 2010 11:23 am

'-f' means awk program is saved in a separate file and argument followed by -f is the name of the awk script file. multiple -f can also be used.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: awk command doubt

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum