Here's a fun one. You can use sed
directly to strip all copies of the first line out and leave everything else in place (including the first line itself).
sed '1{h;n;};G;/^\(.*\)\n\1$/d;s/\n.*$//' input
1{h;n;}
puts the first line into the hold space, prints it, and reads in the next line—skipping the rest of the sed
commands for the first line. (It also skips that first 1
test for the second line, but that doesn't matter as that test wouldn't have applied to the second line.)
G
appends a newline followed by the contents of the hold space to the pattern space.
/^\(.*\)\n\1$/d
deletes the contents of the pattern space (thus skipping to the next line) if the portion after the newline (i.e. what was appended from the hold space) exactly matches the portion before the newline. This is where lines that duplicate the header will get deleted.
s/\n.*$//
deletes the portion of text that was added by the G
command, so that what gets printed is just the line of text from the file.
However, since regex is expensive, a slightly faster approach would be to use the same condition (negated) and P
rint up to the newline if the portion after the newline (i.e. what was appended from the hold space) doesn't exactly match the portion before the newline and then unconditionally delete the pattern space:
sed '1{h;n;};G;/^\(.*\)\n\1$/!P;d' input
Output when given your input is:
ID Data1 Data2
1 100 100
2 100 200
3 200 100
4 100 100
5 200 200