The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-13-2008
repudi8or repudi8or is offline
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.
  #2 (permalink)  
Old 04-13-2008
kholostoi kholostoi is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
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
  #3 (permalink)  
Old 04-13-2008
robsonde robsonde is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 158
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.....
  #4 (permalink)  
Old 04-13-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
  #5 (permalink)  
Old 04-14-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
Or:

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

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

I will use that
Sponsored Links
Closed Thread

Bookmarks

Tags
sed delete line, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 11:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0