1

Is there any way to compare (part of) the hold space against the pattern space for sed? for instance, I have the following lines

name1 id1 12-04-2016
name1 id2 12-03-2016
name2 id3 12-04-2016
name2 id4 12-03-2016

My end goal is to remove the first line where name is duplicated. So my thought process was

1) compare name field to hold space.

2) If it matches, delete line in pattern space

3) If it doesn't, copy line to hold space.

However I cannot find anything that details how to compare what is in the pattern space against what is in the hold space (only how to copy to and from). Is this not possible?

1

It is possible. You can append the content of the hold space to the pattern space and use a backreference to check for equality:

sed -n '{G;s/\([^ ]+\)\(.*\)\n\1//;tl;s/\n//p;:l;n}' names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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