← index

Three awk one-liners I keep forgetting

Sum the third column:

awk '{s += $3} END {print s}' file

Keep the first occurrence of each line, without sorting:

awk '!seen[$0]++' file

Print everything between two markers, exclusive:

awk '/BEGIN/{f=1;next} /END/{f=0} f' file

The second one is the one I look up most. It works because an unset array entry is zero, which is false, and the post-increment makes every later copy true.

For very large files it holds every distinct line in memory. If that matters, sort first and use uniq instead.