How to remove a tagged block of text in a file?
How can I remove all instances of tagged blocks of text in a file with sed, grep, or another program?
If I have a file which contains:
random
text
// START TEXT
internal
text
// END TEXT
more
random
// START TEXT
asdf
// END TEXT
text
how can I remove all blocks of text within the start/end lines, produce the following?
random
text
more
random
text
Solution 1:
sed '\:// START TEXT:,\:// END TEXT:d' file
Solution 2:
The proper way to do this in Perl is with Perl's flip-flop operator
perl -ne'print unless m{^// START TEXT}..m{^// END TEXT}'
x..y in Perl evaluates to true starting with x is true, and ending when y is true. The m{} is another way to write a regular expression match so we don't have to go crazy backslashing all your forward slashes.
Solution 3:
#!/usr/bin/nawk -f
BEGIN {
startblock="^/\/\ START TEXT"
endblock="^/\/\ END TEXT"
}
{
if(! match($0,startblock)) {
{ print }
}
else {
while ( !match($0,endblock )) {
getline;
}
}
}
./removeblocks < sometextfile >anothertextfile
Solution 4:
Perl:
perl -ne '$t=1 if /^\/\/ START TEXT/; print if !$t; $t=0 if /^\/\/ END TEXT/' < sometextfile >anothertextfile