Delete all occurences of a pattern except first

Hi,

Have a flat file like below:
1
2
4
1
2
1
3

Need to delete all duplicate instances.
Output:
1
2
4
3

Can't use "uniq" as it works on a sorted file.
Can't "sort" as need to maintain the original order.

Thanks in advance.

---------- Post updated at 03:51 PM ---------- Previous update was at 03:47 PM ----------

Gotcha
awk ' !x[$0]++'

Actually awk '!x[$1]++' may be more reliable depending on your requirements as it avoids whitespace issues...

perl -i.bak -ne 'print if ! $seen{$_}++;' test.dat has the advantage of doing the deletion in place (and making a backup in case of error)

Can somebody explain how does the awk snippet work :slight_smile: