file.txt abc ccc ddd qqq zzz

Expected result: abc ddd

I tried:

sed -i '/' file.txt 

sed -i '/' file.txt 

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


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

$ sort --unique --key=1,1 file.txt abc 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).

  • 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.