![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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. |
|
||||
|
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 ![]() |
|
||||
|
Quote:
|
|
||||
|
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 |
|
||||
|
much more elegant, thanks radoulov
I will use that |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| sed delete line, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|