113

I have a file which is made up of several lines of text:

The first line
The second line
The third line
The fourth line

I have a string which is one of the lines: The second line

I want to delete the string and all lines after it in the file, so it will delete The third line and The fourth line in addition to the string. The file would become:

The first line

I've searched for a solution on google, and it seems that I should use sed. Something like:

sed 'linenum,$d' file

But how to find the line number of the string? Or, how else should I do it?

4
  • 2
    Your problem statement is contradictory: 'I want to delete all lines after the line' means you delete just two lines (as you say), but then your example output shows the match line as missing too. Which do you actually want? Mar 8, 2011 at 1:58
  • The matching line AND the all the lines after it. I should improve my English. Thanks for your information.
    – DocWiki
    Mar 8, 2011 at 19:24
  • related stackoverflow.com/questions/17364951/… Apr 24, 2019 at 16:49
  • If I don't want to delete the line I search, then how to make it? Dec 10, 2020 at 2:46

6 Answers 6

185

If you don't want to print the matched line (or any following lines):

sed -n '/The second line/q;p' inputfile

This says "when you reach the line that matches the pattern quit, otherwise print each line". The -n option prevents implicit printing and the p command is required to explicitly print lines.

or

sed '/The second line/,$d' inputfile

This says "delete all lines from the output starting at the matched line and continuing to the end of the file".

but the first one is faster. However it will quit processing completely so if you have multiple files as arguments, the ones after the first matching file won't be processed. In this case, the delete form is better.

If you do want to print the matched line, but not any following lines:

sed '/The second line/q' inputfile

This says "print all lines and quit when the matched line is reached" (the -n option (no implicit print) is not used).

See man sed for additional information.

10
  • 3
    But some commands get antsy about broken pipes (RCS co -p for example), and then you are better off with the sed '/The second line/,$d' notation. Mar 8, 2011 at 1:56
  • Can you please add explanations? Jun 1, 2017 at 14:00
  • @AhmadAbdelghany: Explanations added. Jun 1, 2017 at 16:10
  • @DennisWilliamson Thanks a lot. Jun 2, 2017 at 8:52
  • the first method print out the last line before matched line twice. deleting the p after ; works.
    – CrazyFrog
    Sep 29, 2017 at 22:06
43

This is a bit shorter than other given solutions. Quit using capital Q avoids printing the current line.

 sed '/The second line/Q' file

To actually delete the lines you can use the same syntax.

 sed -i '/The second line/Q' file
1
  • 7
    This is by far my favorite solution. Jun 1, 2016 at 23:08
6

Using awk (not showing the matched line)

awk '/pattern/ {exit} {print}' file.txt
1
  • This is the best answer, since you might not know the value of the second line.
    – RonJohn
    Jan 4, 2020 at 19:47
5
sed '/The second line/q0' file

Or, without gnu sed:

sed '/The second line/q' file

Or, using grep:

grep -B 9999999 "The second line"
3
  • Thanks a lot!Can you tell me how I can find the line number of a specific string, which I still dont know.
    – DocWiki
    Mar 8, 2011 at 1:29
  • grep -n "The second line" file | awk -F : '{print $1}'
    – Erik
    Mar 8, 2011 at 1:31
  • @DocWiki: you don't need the line number; you search for it. sed "/$string/,\$d" inputfile. Mar 8, 2011 at 2:00
2

First add the line number and delete the line

cat new.txt 
The first line
The second line
The third line
The fourth line

 cat new.txt  | nl
     1  The first line
     2  The second line
     3  The third line
     4  The fourth line



cat new.txt  | nl | sed  "/2/d"
     1  The first line
     3  The third line
     4  The fourth line

cat new.txt  |  nl |sed  "3d;4d"
     1  The first line
     2  The second line

using awk

awk 'NR!=3 && NR!=4' new.txt 
The first line
The second line
1
awk '/The second line/{exit}1' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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