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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

NR>1

A

Only show the line after x (1 in this case)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

NR==2

A

Only show the second line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

END{print NR}

A

Print the number of records (wc -l)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

NR%2==0
or
NR % 2 == 0

A

Show only the even lines

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

$1==“a” && $2==“b”

A

Match only if both expression are valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly