How to print unique values in order of appearance? How to print unique values in order of appearance? bash bash

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:

  1. Awk processes the file one line at a time and $0 is the current line.
  2. 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 makes count an array and sets the elements to 0 when we first access them.
  3. We increment the count each time we see a particular line.
  4. 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] is 0, and we negate it to !0 == 1. If we see the word again count[$0] is positive, and negating that gives 0.
  5. 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; }.