awk made easy


September 22, 2006

awk ’/REGEX/ {print NR “ $9” $4”_“$5 ;}’ file.txt
supports extended REGEX like perl ( e.g. [:blank:] Space or tab characters )
NR is line number
NF Number of fields
$n is the column to be printed, $0 is the whole row

if it only necessary to print columns of a file it is easier to use cut:

name -a | cut -d” ” -f1,3,11,12

-d: or -d” ” is the delimiter
-f1,3 are the fields to be displayed
other options: -s doesnt show lines without delimiters, –complement is selfesplicative
condition on a specific field:
$<field> ~ /<string>/ Search for string in specified field.

you can use awk also in pipes:
ll | awk ‘NR!=1 {s+=$5} END {print “Average:” s/(NR-1)}’
END to process al file and then print results

tutorial on using awk from the command line: