![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Question for SEDers
Hi Seders,
Given these lines in a file, say file.txt: . . . <tag: ptype>CC<tag: ptype> ( line a ) <tag: pdescr>International<tag: pdescr> ( line b ) <tag: pnum> <tag: pnum> ( line c ) . . . I'd like to change the third line or line c to " <tag:XXXX> <tag:XXXX> " if and only if: line a contains the word CC between the tags "<tag: ptype>" there is a space character between the tags "<tag: pnum>" in line c. Do you know how can i implement this using SED??? (or any other method) Thanks marlonus999 Last edited by marlonus999; 03-05-2007 at 12:03 AM. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
This is part of an XML file... I'm doing it on korn shell. thanks
Last edited by marlonus999; 03-05-2007 at 12:04 AM. |
|
#3
|
|||
|
|||
|
Code:
awk '{ if( $0 ~ "^<tagpnum" ) { if ( $0 ~ ">CC<" ) { print "<tag:XXXX> <tag:XXXX>" } else { print } } else { print } }' file
|
|
#4
|
||||
|
||||
|
Thanks,.. but i think this tests only line c (third line). I need to test the first line if it has a "CC" word on it..if it does...then i'll change the third line.
|
|
#5
|
|||
|
|||
|
Code:
awk 'BEGIN{set=0} { if( $0 ~ "^<tagptype" ) { if ( $0 ~ ">CC<" ) { set=1; print } } else { if ($0 ~ "<tagpnum" ) { print "<tag:XXXX> <tag:XXXX>" } else { print } } }' file
Can be optimized ! |
|
#6
|
||||
|
||||
|
That was great! It worked! Thanks matrixmadhan!
I am just wondering..anyone know a sed version of this??? Please post it, i'll greatly appreciate it |
|
#7
|
|||
|
|||
|
Quote:
Am afraid! Just now tested that and felt ashamed Here is the proper code for both the cases Code:
awk 'BEGIN{set=0} { if( $0 ~ "^<tagptype" && $0 ~ ">CC<" ) { set=1; print } else { if ($0 ~ "<tagpnum" && set == 1 ) { print "<tag:XXXX> <tag:XXXX>" } else { print } } }' file
|
|||
| Google The UNIX and Linux Forums |