grep string & a few lines after

i need to grep a STRING_A & the next few lines after the STRING_A

example file:
STRING_A yada yada
line 1
line 2
STRING_B yada yada
line 1
line 2
line 3
STRING_A yada yada
line 1
line 2
line 3
line 4
STRING_A yada yada
line 1
line 2
line 3
line 4
line 5
line 6

as u can see the line might be vary. sometime 2 lines, sometime 4 or 6 lines. any suggestion?

Scroll towards the end of this page and see the section 'Similar Threads'.

yes i found this thread grep string & next n lines
but those if u have specific next lines, not like this.

One way:

awk '/^STRING_/ && f{f=0} /^STRING_A/{f=1} f' file

Regards

Hi Gurus
I am trying the same type of command through a shell script but it is not working.

$ cat testing.cfg
owner||1
object||1
command_type||1
CREATED_DATE||2
CREATED_BY||2
LAST_UPDATED_DATE||2
LAST_UPDATED_BY||2

$ cat extractedmessage.out
object: RXP_PATIENT
is tag null: Y
command_type: UPDATE
old(2): CREATED_DATE
20-JAN-99
old(3): CREATED_BY
RXP_DM_P001
old(4): LAST_UPDATED_DATE
06-APR-08
old(5): LAST_UPDATED_BY
RXP_DM_P012

p=$\p
cat testing.cfg|while read extractdata
do
whattext=`echo $extractdata|awk -F"||" {'print $1'}`
howmanylines=`echo $extractdata|awk -F"||" {'print $2'}`
echo "sed -n '/${whattext}/,$p' extractedmessage.out|head -${howmanylines}|tail -1"
`sed -n '/${whattext}/,$p' extractedmessage.out|head -${howmanylines}|tail -1` >isitworking.out 2>&1
outputis1=`date`
echo ${outputis1}
done
done

The above script is not working if i put it in a script
cat testing.ksh
p=$\p
cat testing.cfg|while read extractdata
do
whattext=`echo $extractdata|awk -F"||" {'print $1'}`
howmanylines=`echo $extractdata|awk -F"||" {'print $2'}`
echo "sed -n '/${whattext}/,$p' extractedmessage.out|head -${howmanylines}|tail -1"
output1is=`sed -n '/${whattext}/,$p' extractedmessage.out|head -${howmanylines}|tail -1`
echo ${output1is}
done

$ ksh -x testing.ksh
+ p=$p
+ cat testing.cfg
+ read extractdata
+ + echo LAST_UPDATED_DATE||2
+ awk -F|| {print $1}
whattext=LAST_UPDATED_DATE
+ + echo LAST_UPDATED_DATE||2
+ awk -F|| {print $2}
howmanylines=2
+ echo sed -n '/LAST_UPDATED_DATE/,$p' extractedmessage.out|head -2|tail -1
sed -n '/LAST_UPDATED_DATE/,$p' extractedmessage.out|head -2|tail -1
+ + sed -n /${whattext}/,$p extractedmessage.out
+ head -2
+ tail -1
output1is=
+ echo

but if i cut and paste outout of the sed and run it manuall it works fine
$ sed -n '/LAST_UPDATED_DATE/,$p' extractedmessage.out|head -2|tail -1
06-APR-08

Any help will be greatly appreciated.
Thanks in advance
arun

hi gurus,
I am trying to use sed to get certain line after a requested line.
the sed works if i run manually but if i put it through shell it does not work.

Any help will be greatly appreciated.

$ cat bt.ksh
ttt=`sed -n '/LAST_UPDATED_DATE/,$p' extractedmessage.out|head -2|tail -1`

cat testing.cfg|while read extractdata
do
whattext=`echo $extractdata|awk -F"||" {'print $1'}`
howmanylines=`echo $extractdata|awk -F"||" {'print $2'}`
echo $whattext $howmanylines

sed -n '/${whattext}/,$p' extractedmessage.out|head -${howmanylines}|tail -1 >bt.out
echo $tttt
done

< ibmrxcpi-rptdb1 : nwp1 : oracle : /users/oracle/arao >
$ cat testing.cfg
LAST_UPDATED_DATE||2
LAST_UPDATED_BY||2
< ibmrxcpi-rptdb1 : nwp1 : oracle : /users/oracle/arao >
$ cat extractedmessage.out
object: RXP_PATIENT
is tag null: Y
command_type: UPDATE
old(1): PATIENT_ID
3380910
old(2): CREATED_DATE
20-JAN-99
old(3): CREATED_BY
RXP_DM_P001
old(4): LAST_UPDATED_DATE
06-APR-08
old(5): LAST_UPDATED_BY
RXP_DM_P012
< ibmrxcpi-rptdb1 : nwp1 : oracle : /users/oracle/arao >
$ ksh -x bt.ksh
+ + sed -n /LAST_UPDATED_DATE/,$p extractedmessage.out
+ head -2
+ tail -1
ttt=06-APR-08
+ cat testing.cfg
+ read extractdata
+ + echo LAST_UPDATED_DATE||2
+ awk -F|| {print $1}
whattext=LAST_UPDATED_DATE
+ + echo LAST_UPDATED_DATE||2
+ awk -F|| {print $2}
howmanylines=2
+ echo LAST_UPDATED_DATE 2
LAST_UPDATED_DATE 2
+ sed -n /${whattext}/,$p extractedmessage.out
+ head -2
+ tail -1
+ 1> bt.out
+ echo

+ read extractdata
+ + echo LAST_UPDATED_BY||2
+ awk -F|| {print $1}
whattext=LAST_UPDATED_BY
+ + echo LAST_UPDATED_BY||2
+ awk -F|| {print $2}
howmanylines=2
+ echo LAST_UPDATED_BY 2
LAST_UPDATED_BY 2
+ sed -n /${whattext}/,$p extractedmessage.out
+ head -2
+ tail -1
+ 1> bt.out
+ echo

+ read extractdata
< ibmrxcpi-rptdb1 : nwp1 : oracle : /users/oracle/arao >

from the original post:

example file:
STRING_A yada yada
line 1
line 2
STRING_B yada yada
line 1
line 2
line 3
STRING_A yada yada
line 1
line 2
line 3
line 4
STRING_A yada yada
line 1
line 2
line 3
line 4
line 5
line 6

What if you want each of the STRING_A yada yada and its respective following lines sent to a unique file for each?

as such...
STRING_A yada yada
line 1
line 2
sent to somefile1.txt

STRING_A yada yada
line 1
line 2
line 3
line 4
sent to somefile2.txt

and
STRING_A yada yada
line 1
line 2
line 3
line 4
line 5
line 6
sent to somefile3.txt

any help?
Thank you

awk '/^STRING/{c++}{print > "somefile"c".txt"}' file

Regards