Reputation: 724
I would want to only remove the lines between PATTERN1(aaa) and PATTERN2(ccc), excluding the others lines where the patterns matches.
I trying to delete the empty line between PATTERN1(aaa) and PATTERN2(ccc), line 8 in my example...This line will be no all times in same location, that is the reason, why I try to remove it using the 2 patterns.
Essentially it boils down to: "If a blank line is found between aaa and ccc then remove it...
input file
aaa 409
bbb 201
122 0.98
aaa 1.47
aaa 0.00
aaa 0.00
ccc 0.00
121 0.01
135 1.99
output file
aaa 409
bbb 201
122 0.98
aaa 1.47
aaa 0.00
aaa 0.00
ccc 0.00
121 0.01
135 1.99
attempts
sed '/aaa/,/ccc/{//p;d;}' file
sed '/aaa/,/ccc/{//!d}' file
awk '/aaa/{g=1;next}/ccc/{g=0;next}g' file
Thank you in advance.
Upvotes: 0
Views: 117
Reputation: 204548
$ cat tst.awk
/aaa/ { printf "%s", block; block=""; inBlock=1 }
!inBlock { print }
inBlock {
block = block $0 ORS
if ( /ccc/ ) {
gsub(/\n+/,"\n",block)
printf "%s", block
block = ""
inBlock = 0
}
}
END { printf "%s", block }
.
$ awk -f tst.awk file
aaa 409
bbb 201
122 0.98
aaa 1.47
aaa 0.00
aaa 0.00
ccc 0.00
121 0.01
135 1.99
The above removes all blank lines in the block of lines between ccc
and the closest aaa
before it. To isolate that block it starts the block when it sees an aaa
and then restarts it (after printing what was stored as-is) if/when the next aaa
is encountered before a ccc
is encountered.
Upvotes: 2