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
Building /Lib/Modules/$(Uname -R)/Build While Compiling a Kernel
How to Attach Domain Name to My Server
Using Curl with Commands in Go
Starting Multiple Firefox Profiles with Batch File in Linux
User Time Larger Than Real Time
Compile Errors Using Bfd.H on Linux
Where Do You Send The Kernel Console on an Embedded System
How to Type "Cargo Run" Without Needing to Set The Ld_Library_Path Shell Variable
How to Run .Exe Executable File from Linux Command Line
Ssh Command Output to Save in a Text File in Shell Script
Git Clone Using Ssh Failed in Windows Due to Permission Issue
Vagrant, Shared Folder: Take Advantage of Inotify Over Nfs
What Is a Shell Command to Find The Longest Common Substring of Two Strings in Unix
How to Restrict Access to Certain Files for a Certain Process