AWK Frequently Used Snippets Flashcards
1
Q
How are the BEGIN and END statements used in AWK?
A
BEGIN
Use BEGIN to take an action before we even processed the first line.
END
Use END to perform an action after everything has been processed.
END is a good way to summarize information or transform the outcome.
2
Q
NR>1
A
Only show the line after x (1 in this case)
3
Q
NR==2
A
Only show the second line
4
Q
END{print NR}
A
Print the number of records (wc -l)
5
Q
NR%2==0
or
NR % 2 == 0
A
Show only the even lines
6
Q
$1==“a” && $2==“b”
A
Match only if both expression are valid
7
Q
{a[$2]++}END{for(n in a)print n, a[n]}
A
Count items based on value in field 2, then show number of lines with that value