Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Fairly easy. I'm looking for the simplest approaches in both sed and awk for removing the first line in a file that contains a specific string.

share|improve this question

migrated from Apr 11 '13 at 6:14

This question came from our site for professional and enthusiast programmers.

up vote 0 down vote accepted

I guess the easiest solutions then are:


sed -i '0,/string/{//d}' file


awk '/string/ && !p {p++;next}1' file
share|improve this answer

These might work for you:

sed 'x;/./{x;b};x;/string/{h;d}' file


awk '/string/ && !p {p++;next}1' file
share|improve this answer

using gnu sed:

sed '0,/MYSTRING/{//d;}' file # delete only the first match

share|improve this answer
The termination's (;) not needed, is it? – Det Apr 11 '13 at 1:35

Your Answer


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.