Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .