0

file.txt

1.1.1.1 abc 
1.1.1.1 ccc
1.2.2.2 ddd
1.2.2.2 qqq
1.2.2.2 zzz

Expected result:

1.1.1.1 abc
1.2.2.2 ddd

I tried:

sed -i '/1.1.1.1/d' file.txt 

sed -i '/2.2.2.2/d' file.txt 

But however it remove all instead of leaving 1 result. How to delete all matching pattern but left 1?

1 Answers1

1

You can use sort with the --unique option to achieve what you want:

$ sort --unique --key=1,1 file.txt
1.1.1.1 abc
1.2.2.2 ddd

or in short

sort -uk1,1 file.txt

The --unique option suppresses lines with equal fields that were already processed and --key=1,1 defines the first field as sort key (start and end position).

Freddy
  • 25,565