hi all
how to print all lines between "section B (" To the next "section" word that begin in line?
section A (
. .
)
section B (
. .
)
section C (
. .
)
hi all
how to print all lines between "section B (" To the next "section" word that begin in line?
section A (
. .
)
section B (
. .
)
section C (
. .
)
In general if you want to "maintain state" between lines in a file - like you do here - then sed won't do the job. Awk is probably the next-simplest tool you can use in this instance.
The following awk script would do what you want:
/^section\ B\ \(/ {InSection=1; next;}
/^\)/ {InSection=0; next;}
// {if (InSection) print $0;}
sed -n '
/^section B/!n;
/^section B/d;
/^)/q;
p
' yourfile
Explanation of the above sed script, in steps:
!
start with section B
move to the next line.section B
text line.)
we quit.If you want to print everything from "section B" to "section C" including those lines,
sed -ne '/^section B/,/^section/p'
If you don't want to print the two "section" lines,
sed -e '1,/^section B/d' -e '/^section/,$d'
If you want to include "section B" and the closing parenthesis (but not "section C"),
sed -ne '/^section B/,/^)/p'
And there are a few other possible variants.
sed -n '/section B/,/)/p' file
or awk
awk '/section B/,/)/{print}' file
awk -vRS=")" '/section B/{print $0RT}' file