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?
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.
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:
$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 makes count
an array and sets the elements to 0
when we first access them.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
.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; }
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With