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?

0
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).

4
  • Thank you! Im looking for this for ages! – Muzaffar Mohamed Aug 31 '19 at 10:22
  • Is there anyway in sed? – Muzaffar Mohamed Aug 31 '19 at 11:39
  • This might be possible with sed, but it I guess it would be quite complicated. Better use awk or a another method from linked answer. – Freddy Aug 31 '19 at 11:51
  • Thank you... Ill check it out! – Muzaffar Mohamed Aug 31 '19 at 14:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.