Reputation: 103
I wish to remove blank lines(new lines, tabs and spaces) in the file with sed or awk, but only if those blank lines are in between two patterns.
lorem lorem PATTERN1
\t
PATTERN2 lorem2 lorem2`
I expect the result below which is a concatenation of 2 lines with both patterns.
lorem lorem PATTERN1PATTERN2 lorem2 lorem2
Upvotes: 2
Views: 1979
Reputation: 9946
@user537723: you could try awk:
---improved previous post, so it prints on one line between the patterns---
awk '/PATTERN1/{ORS=x} /PATTERN2/{ORS=RS} ORS || NF' file
Upvotes: 0
Reputation: 204638
The GNU awk equivalent of @WilliamPursell's perl script:
awk -v RS='\0' '{print gensub(/(PATTERN1).*(PATTERN2)/,"\\1\\2","g")}' file
Upvotes: 1
Reputation: 58578
This might work for you (GNU sed):
sed -r '/PATTERN1/!b;:a;/PATTERN2/bb;$!{N;ba};:b;s/(PATTERN1.*)[ \t\n]+(.*PATTERN2)/\1\2/;tb' file
/PATTERN1/!b
just print the line unless it contains the first pattern:a;/PATTERN2/bb;$!{N;ba}
read subsequent lines into the pattern space (PS) until the second pattern is encountered:b;s/(PATTERN1.*)[ \t\n]+(.*PATTERN2)/;tb
replace all spaces, tabs and newlines between the first and second patterns.Upvotes: 5
Reputation: 212654
If you just want to delete lines that contain only whitespace between lines that contain PATTERN1 and PATTERN2, just do:
sed '/PATTERN1/,/PATTERN2/{ /^[ \t]*$/d}'
In the sample output you give, it appears that you also want to eliminate the newline that follows PATTERN1, but it's not clear how you want to treat input like:
PATTERN1
non-empty-line
PATTERN2
nor how you want to handle
PATTERN1 non-whitesapce
PATTERN2
Perhaps a clarification of the question is needed. If you really just want to eliminate all whitespace between pattern1
and pattern2
, it's probably easiest with:
perl -0777 -pe 's/(pattern1)\s*(pattern2)/$1$2/g'
Upvotes: 1