Awk
Apr 20, 2007
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
space.
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 $
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 {
} { }
the BEGIN {
before it reads in any data.
awk 'END {print total}'
the END{
data has been read in.
awk '/rule/ {print}'
when you use the
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.
e.g:
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}
{total+=$1}
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.
Labels: Linux/Unix
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home