[H-GEN] replacing text between 2 strings

Jason Parker-Burlingham jasonp at uq.net.au
Wed Jan 8 23:11:13 EST 2003


[ Humbug *General* list - semi-serious discussions about Humbug and     ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]

Shaun Nykvist <shaun at kangawallafox.com.au> writes:

> I have about 200 text files where I need to delete the text between
> multiple occurrences of 2 constant strings.  For example
> Return-Path
> 
> various text here
> 
> Date

I'd do this with something like the following:

        $ perl -lne'if(m/^Return-Path$/){$seen=1;next}
                if(m/^Date$/){$seen=0;next}
                if($seen==1){next}
                print' A

There are more obtuse ways to do it but that's pretty simple, really.

Then Shaun wrote:

> I have managed to solve part of the problem (thanks to those who
> responded), however is there a way to insert a line break or carriage
> return after a specified text string, in a text document.

I assume you mean to insert a blank line after a string?

        $ perl -lpe'm/FIXED/ and $_.="\n"'

This turns:

	foo
	FIXED
	bar

into:
	foo
	FIXED

	bar

Now, as an afterthought, I'm wondering if you're thinking of trying to
parse or reformat email messages?  If so, I hope the messages have a
consistent format, since one usually can't be assured of the order
various headers appear in the message.  There's no shortage of code in
a variety of languages you couldn't reuse instead of reinventing the
wheel.

jason
-- 
``I may have agreed to something involving a goat.''  -- CJ

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.  See http://www.humbug.org.au/



More information about the General mailing list