Search and insert between Pattrens...

Hi Every One...

I wanted to inserted a line in between matched pattrens..

Ex...

InPut File..

       WRITEQ
           TS

**************************
aaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccc
SOME PATTREN's
RESP ( WS-RESP )
END-EXEC

Out Put...

       WRITEQ
           TS

**************************
aaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccc
SOME PATTREN's
dddddddddddddddddddddddd
TSQCHG MAIN
RESP ( WS-RESP )
END-EXEC

I have Tried Some thing of this sort...

/[ ]*WRITEQ/,/[ ]*END-EXEC{ /[ ]*RESP/a\
TSQCHG MAIN/;}

Search for lines between WRITEQ and END-EXEC and on matched ones after RESP insert TSQCHG MAIN
but this doesnot work.... Gives some error while running...

sed: 0602-403 /[ ]*WRITEQ/,/[ ]*END-EXEC{ /[ ]*RESP/a\
TSQCHG MAIN/;} is not a recognized function.

awk ' /WRITEQ/,/END-EXEC/ { if ( $0 ~ /RESP/ ) {print; print "TSQCHG MAIN";next}}; 1' filename
sed "/WRITEQ/,/END-EXEC/{/RESP/s/$/\\
TSQCHG MAIN/;}" filename

that was a beauty....

One small addition...

if RESP is not found can we do it before END-EXEC...(only if RESP not Found)

Example....
WRITEQ
TS
**************************
aaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccc
SOME PATTREN's
END-EXEC

Out Put...

WRITEQ
TS
**************************
aaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccc
SOME PATTREN's
dddddddddddddddddddddddd
TSQCHG MAIN
END-EXEC

awk ' /WRITEQ/,/END-EXEC/ { if ( $0 ~ /RESP/ ) {print; print "TSQCHG MAIN";next}}; 1' filename

i have changed this to

awk ' /WRITEQ/,/END-EXEC/ { if ( $0 ~ /RESP/ ) { print "TSQCHG MAIN";print;next}}; 1' filename

to print TSQCHG MAIN before RESP...
but if the RESP is not found i wanted that to be before "END-EXEC"....

awk ' /WRITEQ/,/END-EXEC/ { if ( $0 ~ /RESP/ ) {print; print "TSQCHG MAIN";next; flag=1}
	if( $0 ~ /END-EXEC/ && flag != 1 ) { print "TSQCHG MAIN"} }; 1' filename

sorry anbu...

i have done a small mistake in giving my requirement...

i wanted "TSQCHG MAIN" to be added before RESP.. not after....
so i have changed the awk u gave as follows..

awk ' /WRITEQ/,/END-EXEC/ { if ( $0 ~ /RESP/ ) {print "TSQCHG MAIN";print; next; flag=1}
if( $0 ~ /END-EXEC/ && flag != 1 ) { print "TSQCHG MAIN"} }; 1' filename

but this inserts at two places...

both before RESP and before END-EXEC...

awaiting ur reply....

Move flag statement before to next statement

awk ' /WRITEQ/,/END-EXEC/ { if ( $0 ~ /RESP/ ) {print "TSQCHG MAIN";print; flag=1; next}
if( $0 ~ /END-EXEC/ && flag != 1 ) { print "TSQCHG MAIN"} }; 1' filename

thanks anbu...

thats works great...