Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract unique lines from a file in order of their first occurence in bash

Tags:

bash

I have a file with a list of strings. I'd like to extract out the unique strings, in the order that they first appear in the file.

So, for instance, if my file contains:

foo
bar
foo
bar
baz
bar
foo

I'd like to output:

foo
bar
baz

If I just wanted the unique values, I could use sort input|uniq, but this sorts my result alphabetically.

like image 874
Scotty Allen Avatar asked Jan 23 '26 18:01

Scotty Allen


1 Answers

Quite simple in awk:

awk '!a[$0]++'
like image 151
Kevin Avatar answered Jan 26 '26 11:01

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!