tags:

views:

91

answers:

4

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 (

. .

)

A: 

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;}
Chris Mead
+1  A: 
sed -n '
  /^section B/!n;
  /^section B/d;
  /^)/q;
  p
' yourfile

Explanation of the above sed script, in steps:

  1. While the line does not ! start with section B move to the next line.
  2. After the we reached the desired line remove the section B text line.
  3. In case the line starts with ) we quit.
  4. Else we print the currently processed line.
mhitza
A: 

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.

Beta
A: 
sed -n '/section B/,/)/p' file

or awk

awk '/section B/,/)/{print}' file

awk -vRS=")" '/section B/{print $0RT}' file
ghostdog74