How to print unique values in order of appearance?
A short, jam-packed awk invocation will get the job done. We'll create an associative array and count every time we've seen a word:
$ awk '!count[$0]++' filegroupswamphandspipesbellyfulemotionafter
Explanation:
- Awk processes the file one line at a time and
$0
is the current line. count
is an associative array mapping lines to the number of times we've seen them. Awk doesn't mind us accessing uninitialized variables. It automatically makescount
an array and sets the elements to0
when we first access them.- We increment the count each time we see a particular line.
- We want the overall expression to evaluate to true the first time we see a word, and false every successive time. When it's true, the line is printed. When it's false, the line is ignored. The first time we see a word
count[$0]
is0
, and we negate it to!0 == 1
. If we see the word againcount[$0]
is positive, and negating that gives0
. - Why does true mean the line is printed? The general syntax we're using is
expr { actions; }
. When the expression is true the actions are taken. But the actions can be omitted; the default action if we don't write one is{ print; }
.