The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-04-2007
marlonus999's Avatar
Registered User
 

Join Date: Dec 2006
Location: Manila
Posts: 27
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.
Reply With Quote
Forum Sponsor
  #2  
Old 03-04-2007
marlonus999's Avatar
Registered User
 

Join Date: Dec 2006
Location: Manila
Posts: 27
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.
Reply With Quote
  #3  
Old 03-05-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Code:
awk '{ if( $0 ~ "^<tagpnum" ) { if ( $0 ~ ">CC<" ) { print "<tag:XXXX> <tag:XXXX>" } else { print } } else { print } }' file
Reply With Quote
  #4  
Old 03-05-2007
marlonus999's Avatar
Registered User
 

Join Date: Dec 2006
Location: Manila
Posts: 27
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.
Reply With Quote
  #5  
Old 03-05-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
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
As per your requirement, the above !


Can be optimized !
Reply With Quote
  #6  
Old 03-05-2007
marlonus999's Avatar
Registered User
 

Join Date: Dec 2006
Location: Manila
Posts: 27
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
Reply With Quote
  #7  
Old 03-05-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
Originally Posted by marlonus999
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

Am afraid! That you didnt check the negative case,

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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0