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.

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

migrated from stackoverflow.com Apr 11 '13 at 6:14

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

3 Answers

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

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

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.