Sponsored Content
Top Forums Shell Programming and Scripting how to delete text from line starting pattern1 up to line before pattern2? Post 302184936 by repudi8or on Sunday 13th of April 2008 07:16:07 PM
Old 04-13-2008
how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :-
Code:
  <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 :-

Code:
  <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.

Last edited by Yogesh Sawant; 05-27-2010 at 04:46 AM.. Reason: added code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep all lines from PATTERN1 to PATTERN2

Hi! From a file like this one : hello ... PATTERN1 ... lines between patterns .. PATTERN2 ... I would like to extract only the lines between patterns, probably with awk I think? Thanks a lot for your help, Tipi (5 Replies)
Discussion started by: tipi
5 Replies

2. Shell Programming and Scripting

SED Exclude Matches Between Pattern1 And Pattern2 Containing Pattern3 Print The Rest

Hi, From the sample file below Conditions 1) Pattern Range must start with "ALTER TABLE" 2) Pattern Range ends when it finds ";" 3) Between this range i want to select all the patterns that contain pattern " MOVE " Note : I would like to exclude the above pattern matches and print... (1 Reply)
Discussion started by: rajan_san
1 Replies

3. Shell Programming and Scripting

Sed Help.To Search Between Pattern1 And Pattern2 Containing Certain Text

Hi, Here is a sample of my Test File $ cat TestFile1 Prompt Table DQZ_ALTER_SCHEMA_ID; ALTER TABLE DQZ.DQZ_ALTER_SCHEMA_ID MONITORING; ALTER TABLE DQZ.DQZ_ALTER_SCHEMA_ID STORAGE ( NEXT 3464K ); Prompt Table DQZ_ALTER_SCHEMA_ID; ALTER TABLE DQZ.DQZ_ALTER_SCHEMA_ID MOVE LOB... (16 Replies)
Discussion started by: rajan_san
16 Replies

4. Shell Programming and Scripting

How to remove a specific line matching pattern1 and pattern2 ?

Hi, I have a file like below: . . . . Jack is going home Jack is going to school Jack is sleeping Jack is eating dinner John is going home John is eating breakfast . . . The specific line is: Jack is going home (2 Replies)
Discussion started by: salih81
2 Replies

5. Shell Programming and Scripting

Search file for pattern2 starting from occurence of pattern1

Hi folks, I have a file which contains several occurences of 2 different patterns. I need to find out the line of first occurence of pattern2 starting after the position of first occurence of pattern1. example file: aaaa pattern2 bbbb pattern1 ccc pattern2 ddd pattern1 eee pattern2... (9 Replies)
Discussion started by: sameucho
9 Replies

6. Shell Programming and Scripting

delete part of file from pattern1 to pattern2

Hi all! How can I delete all the text starting from <string1> to <string2> in all the .txt files of the folder "FOLDER" ? Thanks a lot! mjomba ... </s> <s> <w></w> </s> <s> ... to get: (1 Reply)
Discussion started by: mjomba
1 Replies

7. Shell Programming and Scripting

Append text to line if begins with pattern1 AND does not end with pattern2

Hello, I'm looking for sed solution to change ... <li>keyword</li> <li>keyword <li>keyword</li> <li>keyword <li>keyword</li> ... to ... <li>keyword</li> <li>keyword</li> <li>keyword</li> <li>keyword</li> <li>keyword</li> ... I.e., if lines beginning with <li> do not end with... (3 Replies)
Discussion started by: pioavi
3 Replies

8. Shell Programming and Scripting

Delete Lines : after pattern1 and between pattern2 and pattern3 using awk/sed/perl

Hi I need to delete lines from a file which are after pattern1 and between pattern 2 and patter3, as below: aaaaaaaa bbbbbbbb pattern1 cdededed ddededed pattern2 fefefefe <-----Delete this line efefefef <-----Delete this line pattern3 adsffdsd huaserew Please can you suggest... (6 Replies)
Discussion started by: vk2012
6 Replies

9. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

10. Shell Programming and Scripting

Get lines which has pattern1, pattern2 and pattern3 in it

Version: RHEL 6.5 In the below text file, I want to find the lines which has the string JOHN , KATE and STEVE in it. The logic is to grep with an AND condition ie. get all lines with JOHN AND KATE AND STEVE $ cat sometext.txt PHILIP worked in HR JOHN along with KATE fixed several IT... (4 Replies)
Discussion started by: John K
4 Replies
vi(1)							      General Commands Manual							     vi(1)

Name
       vi - screen editor

Syntax
       vi [ -t tag ] [ +command ] [ -l ] [ -r ] [ -wn ] [ -x ] name...

Description
       The  (visual)  editor is a display-oriented text editor based on The command and the command run the same code.	You can access the command
       mode of from within

       The following is a list of some of the commands.  See the vi Beginner's Reference Card and "An Introduction to Display Editing with vi"	in
       the Supplementary Documents, Volume 1: General User for more details on using

       Screen Control Commands

	    <CTRL/L>	 Reprints current screen.

	    <CTRL/Y>	 Exposes one more line at top of screen.

	    <CTRL/E>	 Exposes one more line at bottom of screen.

       Paging Commands

	    <CTRL/F>	 Pages forward one screen.

	    <CTRL/B>	 Pages back one screen.

	    <CTRL/D>	 Pages down half screen.

	    <CTRL/U>	 Pages up half screen.

       Cursor Positioning Commands

	    j		 Moves cursor down one line, same column.

	    k		 Moves cursor up one line, same column.

	    h		 Moves cursor back one character.

	    l		 Moves cursor forward one character.

	    <RETURN>	 Moves cursor to beginning of next line.

	    0		 Moves cursor to beginning of current line.

	    $		 Moves cursor to end of current line.

	    <SPACE>	 Moves cursor forward one character.

	    nG		 Moves cursor to beginning of line n.  Default is last line  of file.

	    /pattern	 Moves cursor forward to next occurrence of pattern.

	    ?pattern	 Moves cursor backward to next occurrence of pattern.

	    n		 Repeats last / or ? pattern search.

       Text Insertion Commands

	    a		 Appends text after cursor. Terminated by <ESC>.

	    A		 Appends text at the end of the line. Terminated by <ESC>.

	    i		 Inserts text before cursor. Terminated by <ESC>.

	    I		 Inserts text at the beginning of the line. Terminated by <ESC>.

	    o		 Opens new line below the current line for text insertion.  Terminated by <ESC>.

	    O		 Opens new line above the current line for text insertion.  Terminated by <ESC>.

	    <DELETE>	 Overwrites last character during text insertion.

	    <ESC>	 Stops text insertion.

       Text Deletion Commands

	    dw		 Deletes current word.

	    x		 Deletes current character.

	    dd		 Deletes current line.

	    D, d$	 Deletes from cursor to end of line.

	    P		 Puts back text from the previous delete.

       Text Change Commands

	    cw		 Changes characters of current word until stopped with escape key.

	    c$		 Changes text up to the end of the line.

	    C, c$	 Changes remaining text on current line until stopped by pressing the escape key.

	    ~		 Changes case of current character.

	    xp		 Transposes current and following characters.

	    J		 Joins current line with next line.

	    rx		 Replaces current character with x.

       Buffer Usage Commands

	    [a-z]n yy	 Yanks n lines to the [a-z] buffer.  Default is current line.

	    [a-z]n p	 Puts n yanked text lines from the a-z buffer, after the cursor.

       Exiting vi

	    ZZ		 Exits and saves changes

	    :wq 	 Writes changes to current file and quits edit session.

	    :q		 Quits edit session (no changes made).

       The command uses all of the same edit or commands as However, does not allow you to write the file. See

Options
       -t tag	 Specifies a list of tag files.  The tag files are preceded by a backslash () and are separated by spaces.  The tag option should
		 always be the first entry.

       +command  Tells the editor to begin by executing the specified command.	A useful example would be +/pattern to search for a pattern.

       -l	 Sets the showmatch and lisp options for editing LISP code.

       -r name	 Retrieves the last saved version of the name'd file in the event of an editor or system crash.  If no file is specified,  a  list
		 of saved files is produced.

       -wn	 Sets  the default window size to n.  This option is useful for starting in a small window on dialups.	The -x option is available
		 only if the Encryption layered product is installed.

       -x	 Causes to prompt for a key.  The key is used to encrypt and decrypt the contents of the file. If  the	file  contents	have  been
		 encrypted with one key, you must use the same key to decrypt the file.

Restrictions
       Software tabs using ^T work only immediately after the autoindent.

       Left and right shifts on intelligent terminals do not make use of insert and delete character operations in the terminal.

       The  wrapmargin option sometimes works incorrectly because it looks at output columns when blanks are typed.  If a long word passes through
       the margin and onto the next line without a break, then the line is not broken.

       Insert/delete within a line can be slow if tabs are present on intelligent terminals, since the terminals need  help  in  doing	this  cor-
       rectly.

       Saving text on deletes in the named buffers is somewhat inefficient.

       The  source  command does not work when executed as :source; there is no way to use the :append, :change, and :insert commands, since it is
       not possible to give on a :global you must Q to command mode, execute them, and then reenter the screen editor with or

See Also
       ed(1), ex(1), view(1)
       The Little Gray Book: An ULTRIX Primer
       The Big Gray Book: The Next Step with ULTRIX
       "An Introduction to Display Editing with Vi", Supplementary Documents, Volume 1: General User

																	     vi(1)
All times are GMT -4. The time now is 10:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy