Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print unique values in order of appearance?

Tags:

bash

unique

I'm trying to get the unique values from the list below, but leaving the unique values in the original order.

This is, the order of appearance.

group
swamp
group
hands
swamp
pipes
group
bellyful
pipes
swamp
emotion
swamp
pipes
bellyful
after
bellyful

I've tried combining sort and uniq commands but the output is sorted alphabetically, and if I don't use sort command, uniq command doesn't work.

$ sort file | uniq
after
bellyful
emotion
group
hands
pipes
swamp

and my desiree output would be like this

group
swamp
hands
pipes
bellyful
emotion
after

How can I do this?

like image 625
Ger Cas Avatar asked Mar 15 '20 00:03

Ger Cas


People also ask

How do you sort unique values?

To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates. To highlight unique or duplicate values, use the Conditional Formatting command in the Style group on the Home tab.


1 Answers

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]++' file
group
swamp
hands
pipes
bellyful
emotion
after

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; }.
like image 82
John Kugelman Avatar answered Nov 15 '22 04:11

John Kugelman