How to Delete All Lines in a File Starting from After a Matching Line

How do I delete all lines in a file starting from after a matching line?

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.


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.

How to delete from a text file, all lines that contain a specific string?

To remove the line and print the output to standard out:

sed '/pattern to match/d' ./infile

To directly modify the file – does not work with BSD sed:

sed -i '/pattern to match/d' ./infile

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

sed -i '' '/pattern to match/d' ./infile

To directly modify the file (and create a backup) – works with BSD and GNU sed:

sed -i.bak '/pattern to match/d' ./infile

How to remove all lines after a line containing some string?

awk '{print} /fox/{exit}' file

With GNU sed:

sed '0,/fox/!d' file


sed -n '0,/fox/p' file

how to delete all the lines after a certain line in shell?

If you want to remove from the match of ^second line to end of file, you will want to force an exit with q after the match, e.g.

sed '/^second line/q' file

(you can add -i to edit in-place)

Example Use/Output

With your lines in file:

$ sed '/^second line/q' file
first line
second line

Use sed to delete all lines starting with pattern b after line with pattern a

With GNU sed, you may use

sed '/DELETE ME/{:a;N;s/\n[[:blank:]]*-.*//;ta;!P;D}' file

See the online sed demo:

s='first line
second line DELETE ME
- third line
- fourth line
fifth line
sixth line DELETE ME
seventh line
- eighth line'
sed '/DELETE ME/{:a;N;s/\n[[:blank:]]*-.*//;ta;!P;D}' <<< "$s"


first line
fifth line
seventh line
- eighth line


  • /DELETE ME/ - finds all lines that contain DELETE ME string
  • {:a;N;s/\n[[:blank:]]*-.*//;ta;!P;D} - if the line matching DELETE ME is found, this block is entered:
    • :a - an a label marks the current position
    • N - reads the next line with \n at the start into the pattern space
    • s/\n[[:blank:]]*-.*// - finds and removes the newline, 0+ blank chars, - and the rest of the string
    • ta - if the substitution occurred, sed goes to the position marked with a
    • !P - otherwise, prints the pattern space content until the first newline (i.e. prints the first line)
    • D - deletes the pattern space content until the first new line, i.e. deletes the first line inside pattern space, and restarts cycle with the resultant pattern space, without reading a new line of input.

Remove all lines before a match with sed

try this (GNU sed only):

sed '0,/^bin$/d'

..output is:

$sed '0,/^bin$/d' file

Delete all lines beginning with a # from a file

This can be done with a sed one-liner:

sed '/^#/d'

This says, "find all lines that start with # and delete them, leaving everything else."

How to delete all lines before the first and after the last occurrence of a string?

Could you please try following. Written and tested with shown samples with GNU awk.

awk '
found && NF{
val=(val?val ORS:"")$0
match(val,/.*Lecture [0-9]+/)
print substr(val,RSTART,RLENGTH)
}' Input_file

Explanation: Adding detailed explanation for above.

awk '                                        ##Starting awk program from here.
/Lecture/{ ##Checking if a line has Lecture keyword then do following.
found=1 ##Setting found to 1 here.
found && NF{ ##Checking if found is SET and line is NOT NULL then do following.
val=(val?val ORS:"")$0 ##Creating va and keep adding its value in it.
END{ ##Starting END block of this code here.
if(val){ ##Checking condition if val is set then do following.
match(val,/.*Lecture [0-9]+/) ##Matching regex till Lecture digits in its value.
print substr(val,RSTART,RLENGTH) ##Printing sub string of matched values here to print only matched values.
}' Input_file ##Mentioning Input_file name here.

Delete all lines before first occurrence of specific string in file

With sed you could use:

sed -i '/somestring/,$!d' file

Explanation of replace expressions:

, matches lines starting from where the first
address matches, and continues until the second match

$ matches the last line of the last file of input,
or the last line of each file when the -i or -s options are

! If the character follows an address range, then only lines
which do not match the address range will be selected.

d Delete the pattern space; immediately start next cycle.


$ sed -i '/somestring/,$!d' file

Related Topics

Leave a reply