Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete text between newlines

Status
Not open for further replies.

Reginald0

IS-IT--Management
Apr 18, 2002
28
BR
Hi, folks!

I'm trying to delete text between newlines using Perl in Windows command line, like this:

C:\> type file.txt | perl -pe "s/Text1.*\n.*\nText2//"

It's not working, but works if I try with just one line:

C:\> type file.txt | perl -pe "s/Text1.*\n//"

Is there some way to make it work? Thanks in advance!

Reginald0
 
this is an example of how i would do it without the command line:-

Code:
[b]#!/usr/bin/perl[/b]

undef $/;
$_ = <DATA>;
$/ = "\n";

s/Text1.*Text2//s;

print;

[blue]__DATA__
blah1 blah2 blah3
blah4 Text1 blah5 blah6
blah7 blah8
blah9 blah10 blah11
blah12 Text2 blah13
blah14 blah15[/blue]

... it relies on the regex s switch


Kind Regards
Duncan
 
Hi, folks!

Sorry, I found a solution based on duncdude tip and forgot to post it here at that time, but better late than never :)

C:\> type file.txt | perl -0ne "s/Text1.*\n.*\nText2//s; print"

Many thanks, duncdude!

Reginald0
 
Sorry again, the solution was even simpler (I think my memory has some damaged component :))

C:\> type file.txt | perl -0pe "s/Text1.*\n.*\nText2//s"

Reginald0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top