How to Delete 5 Lines Before and 6 Lines After Pattern Match Using Sed

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

Deleting n lines in both directions and the match in sed?

sed operates on a range of addresses. That means either one or two expressions, not three.

/match/ is an address which matches a regex.

-2 is an address which specifies two lines before

+2 is an address which specifies two lines after

Therefore:

/match/,-2 is a range which specifies the line matching match to two lines before.

/match/-2,+2d, on the other hand, includes three addresses, and thus makes no sense.

To delete two lines before and after a pattern, I would recommend something like this (modified from this answer):

sed -n "1N;2N;/\npattern$/{N;N;d};P;N;D"

This keeps 3 lines in the buffer and reads through the file. When the pattern is found in the last line, it reads two more lines and deletes all 5. Note that this will not work if the pattern is in the first two lines of the file, but it is a start.

Remove all lines before a match with sed

try this (GNU sed only):

sed '0,/^bin$/d'

..output is:


$sed '0,/^bin$/d' file
boot
...
sys
tmp
usr
var
vmlinuz

Delete n lines after the first match in sed

Do it with awk.

When you match the first test 1, set a variable and skip the next N lines with getline.

Print the line unconditionally once the variable is set.

awk -v nlines=2 '
found {print; next}
/test 1/ { found = 1; for (i = 0; i < nlines; i++) getline }' file.txt

Delete lines before and after a match in bash (with sed or awk)?

sed will do it:

sed '/\n/!N;/\n.*\n/!N;/\n.*\n.*PINITIAL BALANCE/{$d;N;N;d};P;D'

It works this way:

  • if sed has only one string in pattern space it joins another one
  • if there are only two it joins the third one
  • if it does natch to pattern LINE + LINE + LINE with BALANCE it joins two following strings, deletes them and goes at the beginning
  • if not, it prints the first string from pattern and deletes it and goes at the beginning without swiping the pattern space

To prevent the appearance of pattern on the first string you should modify the script:

sed '1{/PINITIAL BALANCE/{N;N;d}};/\n/!N;/\n.*\n/!N;/\n.*\n.*PINITIAL BALANCE/{$d;N;N;d};P;D'

However, it fails in case you have another PINITIAL BALANCE in string which are going to be deleted. However, other solutions fails too =)

SED: delete lines after pattern match while skipping a few lines, then delete the pattern line

Another approach:

sed '/($(ose),)/,/^endif/{/($(ose),)/d;/^else/,/^endif/d}' file

Output:


...
dh_installdocs \
$(archdir)/UserManual*.pdf $(archdir)/VirtualBox*.chm \
$(addprefix $(archdir)/, LICENSE)
rm $(addprefix $(archdir)/,UserManual*.pdf VirtualBox*.chm \
LICENSE)
...

Add option -i to edit "in place".



Related Topics



Leave a reply



Submit