Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-04-2007, 07:18 AM   #1
LQ Newbie
Registered: May 2007
Posts: 1

Rep: Reputation: 0
sed: delete text till <pattern2> depending on length of text

Hello all,

Probably a simple thing to do, but I havent used sed much ..

I would like to delete all lines between <pattern1> and the first occurence of <pattern2> repeatedly but only if <pattern2> occurs n lines below <pattern1> ... any idea how to do that in sed ?


Old 05-05-2007, 12:49 AM   #2
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
will delete all lines between PATTERN1 and PATTERN2
1 members found this post helpful.
Old 05-05-2007, 01:40 AM   #3
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
This sounds a bit more complex that what I've done with sed.
I think you could use the pattern range of of /PATTERN1/,/PATTERN2/ and have a subpattern match test for the lines in between. If the line matches PATTERN1, add the line to the multiline buffer. If that test fails and the line doesn't match PATTERN2, it is a line between the patterns, so add that also to the multiline buffer. If it matches PATTERN2, then test the multiline buffer for n return characters and delete it if it matches.
  /PATTERN2/s/<multiline pattern>//d;n
and then use a subrange so that if the pattern on the line being read is PATTERN1 or neither pattern, add the line to the buffer. If the line being read is PATTERN2, then use a search/replace that matches [b]n[b] return characters.

I'm not certain from your question whether you want to delete an inclusive range or an exclusive range, that is to say, delete everything between the patterns or delete the patterns as well. Also, I'm not certain if by n lines you mean exactly n lines or at least n lines.

I think a pattern like PATTERN1([^\n]*\n[^\n]*){[b]n[\n]}PATTERN2 if you use the extended regex option might do for the substitution test.

The two PATTERNs on the ends can serve as anchors so that a pattern with more than [b]n[b] return characters won't match.


I'll admit that I supplied a rough idea only, and it isn't tested at all. There are often cases pop up during testing that can throw a monkey wrench into the works. Such as if PATTERN1 or PATTERN2 might be split between lines. When using the 'n' or 'N' command, sometimes being on the last line in the file can goof things up.


Here is a link to an online version of the first edition of "Sed & Awk".

For learning awk, the gawk source package gives you access to the docbook source of "Gawk: Effective Awk Programming".
If it is an rpm, installing the patches "rpmbuild -bp gawk.spec", cd'ing to the BUILD/gawk-<version>/ directory and running:
make pdf

Will give you a pdf version of this book. It is excellent. I wish the sed package supplied one as good. The sed manual has strange examples such as emulating 'tac' or a calulator. Practical examples such as the one you presented would have been much better.

It's one thing to be thown in the deep end---cliff diving is not to way to learn how to swim.

Last edited by jschiwal; 05-05-2007 at 03:20 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
Delete text between key words : sed shalomajay Programming 7 05-11-2011 06:22 PM
How to Increase Length of the Icon Text KimInWis SUSE / openSUSE 5 12-04-2006 07:38 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
storing text in a text file for a specified length of time. mrobertson Programming 7 08-02-2005 10:27 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:06 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration