Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to delete every line that contains a pattern after the first line (the pattern is /^country\t/). I can delete every occurrence with sed '/^country\t/d' in.txt > out.txt, but I would like to keep the first occurrence on the first line.

The back story is that I have a directory of tab-delimited files with headers and I concatenate these files with cat. I would like to remove the extra headers.

In Vim I could do something like : 2,$g/^country\t/d, but I can't figure out the sed solution. When I try sed '2,$ /^country\t/d in.txt > out.txt I get the error unknown command: /.

Thanks!

share|improve this question

3 Answers 3

up vote 1 down vote accepted

sed -e '1,2 s/^country\t/NOCHANGE/' -e '/^country\t/d' -e 's/NOCHANGE/country/' in.txt > out.txt

no direct way, but that would be a workaround.

share|improve this answer
sed 's/[^country]*//2' <old >new

should change all the instances of country except for the first one (if you have 2 on the first line only the first one will be preserved)

PS: I MAY HAVE MISSED A SPACE IN THERE. I AM NOT ON MY MAIN BOX SO I CANNOT CHECK

share|improve this answer
    
Thanks, but I would like to delete the whole line. Is my only choice using the s///g construct and making the pattern something like /^country.*\n/? –  Richard Herron Jun 5 '12 at 17:32
1  
Check this out: grymoire.com/Unix/Sed.html –  Bruno9779 Jun 5 '12 at 17:44
    
+1, good link! (I had found that in my searches, but couldn't piece together @johnshen64's hack.) –  Richard Herron Jun 5 '12 at 18:06

This might work for you:

sed '1b;/^country\t/d' in.txt > out.txt

or

sed '1!{/^country\t/d}' in.txt > out.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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