[ACCEPTED]-excluding first and last lines from sed /START/,/END/-text-manipulation
This should do the trick:
sed -e '/=sec1=/,/=sec2=/ { /=sec1=/b; /=sec2=/b; s/^/#/ }' < input
This matches between 10 sec1 and sec2 inclusively and then just 9 skips the first and last line with the b
command. This 8 leaves the desired lines between sec1 and 7 sec2 (exclusive), and the s
command adds 6 the comment sign.
Unfortunately, you do need 5 to repeat the regexps for matching the delimiters. As 4 far as I know there's no better way to do 3 this. At least you can keep the regexps 2 clean, even though they're used twice.
This 1 is adapted from the SED FAQ: How do I address all the lines between RE1 and RE2, excluding the lines themselves?
If you're not interested in lines outside 8 of the range, but just want the non-inclusive 7 variant of the Iowa/Montana example from 6 the question (which is what brought me here), you 5 can write the "except for the first and 4 last matching lines" clause easily enough 3 with a second sed:
sed -n '/PATTERN1/,/PATTERN2/p' < input | sed '1d;$d'
Personally, I find this 2 slightly clearer (albeit slower on large 1 files) than the equivalent
sed -n '1,/PATTERN1/d;/PATTERN2/q;p' < input
Another way would be
sed '/begin/,/end/ {
/begin/n
/end/ !p
}'
/begin/n
-> skip over the 3 line that has the "begin" pattern
/end/ !p
-> print 2 all lines that don't have the "end" pattern
Taken 1 from Bruce Barnett's sed tutorial http://www.grymoire.com/Unix/Sed.html#toc-uh-35a
I've used:
sed '/begin/,/end/{/begin\|end/!p}'
This will search all the lines 2 between the patterns, then print everything 1 not containing the patterns
you could also use awk
awk '/sec1/{f=1;print;next}f && !/sec2/{ $0="#"$0}/sec2/{f=0}1' file
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.