The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to delete line with matching text and line immediately after orahi001 UNIX for Dummies Questions & Answers 6 01-14-2008 08:34 PM
Delete line in text file Berserk UNIX for Dummies Questions & Answers 5 11-07-2007 12:46 PM
Delete first line from any text file ? aungomarin Shell Programming and Scripting 5 05-16-2006 06:42 PM
delete a line based on first character of the line borncrazy UNIX for Dummies Questions & Answers 2 12-06-2005 11:27 AM
delete last line from text file hcclnoodles Shell Programming and Scripting 4 06-25-2002 06:52 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-13-2008
Registered User
 

Join Date: Apr 2008
Posts: 11
how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :-

<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"/>

I want to delete line containing mybag2 and its subsequent contents (number of contents lines can vary). Thus I wish to delete from pattern mybag2 up to (but not including) the next "bag name" tag and result in :-

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

I have tried this a few different ways with sed and awk and have yet to find a solution. Any help would be appreciated.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-13-2008
Registered User
 

Join Date: Mar 2008
Posts: 5
This is my solution:

---------file : awk_tets --------------
BEGIN { flag = 0}
/^<bag name="mybag2"/ { flag = 1}
/^<bag name="mybag3"/ { flag = 0}
{ if (flag == 0) { print; } }
---------------------------------------

in command line: $ awk -f awk_test your_data_file > result

Let's try it
Reply With Quote
  #3 (permalink)  
Old 04-13-2008
Registered User
 

Join Date: Dec 2007
Posts: 46
Quote:
Originally Posted by kholostoi View Post
This is my solution:

---------file : awk_tets --------------
BEGIN { flag = 0}
/^<bag name="mybag2"/ { flag = 1}
/^<bag name="mybag3"/ { flag = 0}
{ if (flag == 0) { print; } }
---------------------------------------

in command line: $ awk -f awk_test your_data_file > result

Let's try it
this is what i had in mind too, problem is that it may not handel nested ceses too well.....
Reply With Quote
  #4 (permalink)  
Old 04-13-2008
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
Reply With Quote
  #5 (permalink)  
Old 04-14-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,391
Or:

Code:
awk '/^<bag name=/{f=0}$0~v{f=1}!f' v="mybag2" file
Use nawk or /usr/xpg4/bin/awk on Solaris.
Reply With Quote
  #6 (permalink)  
Old 04-15-2008
Registered User
 

Join Date: Apr 2008
Posts: 11
much more elegant, thanks radoulov

I will use that
Reply With Quote
Google UNIX.COM
Reply

Tags
solaris

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:44 PM.


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

Content Relevant URLs by vBSEO 3.2.0