1819814 Members
4156 Online
109607 Solutions
New Discussion

Re: Awk examples !!

 
Whitehorse_1
Frequent Advisor

Awk examples !!

Hi Admins,

Can somebody gimme good examples (not man pages) for 'awk' usage...

Thxs, WH
Reading is a good course medicine for deep sleep !!
13 REPLIES 13
Peter Godron
Honored Contributor

Re: Awk examples !!

Hi,
not clear what you are after, however...

Example of awk useage:
http://www.student.northpark.edu/pemente/awk/awk1line.txt

And for further study:
http://en.wikipedia.org/wiki/AWK_programming_language
Piergiacomo Perini
Trusted Contributor

Re: Awk examples !!

Hi WH,

a little example:

awk â Fâ â â { print $1, $2, $3 }â file_name


-F : Field Separator (in this case is â blankâ ).
So you can select first three column from "file_name" (in "file_name" data are formatted in columns).

Hth
regards
pg
Piergiacomo Perini
Trusted Contributor

Re: Awk examples !!

i beg your pardon, here the right command :

awk â F´ ` `{ print $1, $2, $3 }´ file_di_input
Piergiacomo Perini
Trusted Contributor

Re: Awk examples !!

_today is NOT a good day, for me_

awk -  F´ ` `{ print $1, $2, $3 }´ file_di_input
OldSchool
Honored Contributor

Re: Awk examples !!

you might also try to get ahold of the book "sed & awk" published by o'reilly
Pete Randall
Outstanding Contributor

Re: Awk examples !!

Attached are some examples.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Awk examples !!

Hi:

I'm not convinced that examples in the abstract are going to be that helpful.

A nice tutorial (*with* examples *and* explanations) can be found here:

http://www.gnu.org/software/gawk/manual/gawk.html

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Awk examples !!

Since you don't even bother to explain what you are trying to do, it's difficult to be very helpful. In any event, a simple search of the Forums for "awk" should yield hundreds (at least) of examples ranging from trivial one-liners to very complex awk scripts.

If you are learning from scratch, I might suggest a Plan B: learn Perl. The learning curve is steeper but the power is much greater.

The O'reilly "sed & awk" is hard to beat and it has a good sectio on regular expressions. RE's are really the key to effectively using awk, sed, or Perl. Perl's RE's are RE's on steroids and make things that might require several steps in awk doable in one step.
If it ain't broke, I can fix that.
IT_2007
Honored Contributor

Re: Awk examples !!

Here is if you want to grab only volume group names from /etc/lvmtab

strings /etc/lvmtab |grep vg |awk -F "\/" '{print $3}'

which will print only name of volume groups (excludes /dev path)
Arturo Galbiati
Esteemed Contributor

Re: Awk examples !!

Hi,

# Show mount points over PCT
bdf|awk -v PCT=90 'NR==1||substr($5,1,index($5,"%")-1)+0>=PCT'
*

# Remove empty files
rm -f $(ll|awk '$9&&!$5 {print "rm -f " $9}')

# See all active Oracle databases info
awk -F: '!/^[#|*]/&&!/^$/&&$3="Y"' /etc/oratab

HTH,
Art
sajeer_2
Regular Advisor

Re: Awk examples !!


Small one

To print user names from /etc/passwd file,

awk -F ":" '{ print $1 }' /etc/passwd

Fred Martin_1
Valued Contributor

Re: Awk examples !!

I know you asked for no man pages, etc, however, the book called 'The AWK Programming Language' by AWK's authors is one of the best software manuals ever writtn. Everything you need to know and nothing more than you need.

And, happily, it starts with a simple example and builds to complex programs.

I expect by the time you finish page 30 will be writing just about anything you need, including the one-liners used so often in shell scripts.

Fred

The AWK Programming Language
by Aho, Kernighan and Weinberger
Addison-Wesley Publishing Company
fmartin@applicatorssales.com
Geoff Wild
Honored Contributor

Re: Awk examples !!

google "awk examples" reveals over 2000 pages :)

This one has quite a few:

http://sparky.rice.edu/~hartigan/awk.html

Chapter 4 of "Unix Unleashed Internet" is my fav:

http://docs.rinet.ru/UNIXi/ch04.htm

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.