LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-04-2007, 07:18 AM   #1
oyarsamoh
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 ?

thanks!

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

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

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
Code:
/PATTERN1/,/PATTERN2/{
  /PATTERN1/N;
  /PATTERN2/!N;
  /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".
http://doc.novsu.ac.ru/oreilly/unix/sedawk/index.htm

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:
./configure
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 PM.

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