Francais | Espanol | Deutsche | Italiano | 中文 | 한국어 | 日本語 |

This blog is owned by
Amey Palyekar
software engineer by profession


Add to Technorati Favorites
~My Links~



October 2006
April 2007


Computer Blogs -  Blog Catalog Blog Directory



Apr 20, 2007
Awk language is very powerful component of unix system.
It is used for data manipulation tasks, such as extracting fields from
a line of input.

The awk language makes assumption about the format of its input
which provide you with the ability to make simple programs. It
assumes input is ASCII text, input can be organized into lines
or records, records can be organized into fields. An awk processes file
line by line, each line is treated individually and is considered to be
a record. Each record is collection of fields that are separated by white

awk '{print $1}' dogs-passage
This command print out the first field in every line in file dogs-passage.
In action statement {print $1}, refers to field using $, where is field number
you are interested in.

awk '{print $1,$2}' dogs-passage
This prints first two fields

Programs in awk contain pattern-action statements in one of these formats,
  • BEGIN{}
  • END {}
  • {}
awk 'BEGIN {total=5}'
the BEGIN {} format allows you to specify an action for awk to perform,
before it reads in any data.

awk 'END {print total}'
the END{} format allows you to specify action that are performed after all
data has been read in.

awk '/rule/ {print}'
when you use the {action} format, awk executes action whenever pattern
is matched.

You can specify more than one pattern if you want to specify range of values.
, {}

you can exclude either of pattern or action. If you exclude pattern part, specified
action is executed on every line
awk '{print $1}'

If you exclude action part, awk simply prints out each record within pattern range
awk '{print}'

If pattern does not match awk does not execute the action, action part tells
what to carry out.

awk '/hear/' dogs-passage
finds lines in file 'dogs-passage' that contains particular word 'hear', syntax is //

awk '/hear/{print}' dogs-passage
prints the line containing word hear from file 'dogs-passage'

awk 'BEGIN {total=5}
END {print "Total=" total}' file-numbers

output: Total=794

above command first initializes total to 5 in BEGIN, then adds numbers from
first field in every line from file 'file-numbers' with total, and at last prints the
total in END.



Post a Comment

Subscribe to Post Comments [Atom]

<< Home