How to Delete The Line That Matches a Pattern and The Line After It with Sed

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

sed or awk: delete n lines following a pattern

I'll have a go at this.

To delete 5 lines after a pattern (including the line with the pattern):

sed -e '/pattern/,+5d' file.txt

To delete 5 lines after a pattern (excluding the line with the pattern):

sed -e '/pattern/{n;N;N;N;N;d}' file.txt

Delete to end of line after a match, keep lines not matched

You need to leave out the -n option.
Check out http://www.gnu.org/software/sed/manual/sed.html#index-g_t_002d_002dquiet-7

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"

Output:

first line
fifth line
seventh line
- eighth line

Details

  • /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.

How to delete the line that matches a pattern and the line after it with sed?

Using an extension of the GNU version of sed:

sed -e '/FLAG/,+1 d' infile

It yields:

good text
good text
good text
good text
good test
good text

SED to remove a Line with REGEX Pattern

$ sed '/^A.*lorem$/d' file.txt
  • ^A: starts with an A
  • .*: stuff in the middle
  • lorem$: ends with lorem

sed - Remove previous line and current line based on pattern

This might work for you (GNU sed):

sed 'N;/\n.*999/d;P;D' file

Open a running window of two lines throughout the length of the file.

If the second line of the window contains 999 delete both lines.

Otherwise, print the first line of the window, delete the first line and repeat.

An alternative solution for line 1 or 2 or more contiguous lines containing 999:

sed -n ':a;$!N;/\n.*999/{:b;n;/999/bb;ba};/999/!P;D' file

Delete line that matches pattern and all subsequent lines until second pattern

You may use

sed '/delete,/,/deletion\./d' file > outfile

Here,

  • /delete,/,/deletion\./ tells sed to match a portion of text between (and including) the lines, starting with one contraining delete, and ending with the line having deletion. (note . must be escaped to match a literal dot)
  • d tells sed to remove that block of lines.

See an online sed demo.



Related Topics



Leave a reply



Submit