Friday, April 2, 2010

All about awk

Awk script format    awk ’/regular expression/ { print }’ filename
/pattern1/ { actions… }
/pattern2/ { actions… }
BEGIN { actions… } these actions happen before input is read!
END { actions… }    these actions happen after all the input is read!                                                             

Awk creates the variables $1, $2, $3… that correspond to the resulting fields (just like a shell script).
$0 is the entire line.
Awk automatically splits each line of input on the field separator FS (by default whitespace).

NF number of fields in the current record or the last word in the line
NR number of the current record (line)

No comments: