1758586 Members
1906 Online
108872 Solutions
New Discussion юеВ

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)