The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 04-14-2008
repudi8or repudi8or is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 11
I tried that solution :-

me@myserver $ nawk ' BEGIN { flag = 0} /^<bag name="mybag2"/ { flag = 1} /^<bag name="mybag3"/ { flag = 0} { if (flag == 0) { print; }}' test2

<bag name="mybag1" version="1.0"/>
<contents id="coins"/>
<bag name="mybag3" version="1.6"/>

It works, but the issue is that the second bag name could be any value, its not specifically "mybag3", so the second pattern must be the more generic "/^bag name=/

So i tried that also :-

me@myserver $ nawk ' BEGIN { flag = 0} /^<bag name="mybag2"/ { flag = 1} /^<bag name=/ { flag = 0} { if (flag == 0) { print; }}' test2

<bag name="mybag1" version="1.0"/>
<contents id="coins"/>
<bag name="mybag2" version="1.1"/>
<contents id="clothes"/>
<contents id="shoes"/>
<bag name="mybag3" version="1.6"/>

it failed because the flag was being reset straight after it was set as the more generic pattern also matched the mybag2 line.

Then I switched the pattern order and BINGO !

me@myserver $ nawk ' BEGIN { flag = 0} /^<bag name=/ { flag = 0} /^<bag name="mybag2"/ { flag = 1} { if (flag == 0) { print; }}' test2<bag

name="mybag1" version="1.0"/>
<contents id="coins"/>
<bag name="mybag3" version="1.6"/>

Thanks very much for leading me in the right direction kholostoi