Awk

Awk operates on records (lines by default) and fields (words by default) It reads files one line by line and splits the lines into words

Pattern: stuff to operate on {action [field]}

FS - field separator (space by default) RS - Record separator (newline by default) NR - number of input records NF - number of input fields for the current record OFS - output file separator ORS

$0 - the whole record $1 - the first field $2 - second

awk '{print $0}' file1 file2

## Main usage
# awk [condition] [what to output] <filename>

# print lines 3 through 6
awk 'NR==3, NR==6 {print NR,$0}' employee.txt

# print the number of lines
awk 'END { print NR }' file
# identify the 7th field
awk -F, '{print $7}' file.csv
awk '{ printf "%-30s %-30s %-30s\n", $1, $2, $3}'

Writing an Awk script

#!/usr/bin/awk -f

Patterns

/frodo/ ? /ring/ : /orcs/{print "something"} # ternary
/frodo/ , /ring/ {print $0} # print all of the lines between frodo and ring