Check: man perlvar

Here is a cut & paste for doing multi line matches...

$MULTILINE_MATCHING
$*      Set to a non-zero integer value to do multi-line matching within a string, 0 
(or undefined) to tell Perl that
        it can assume that strings contain a single line, for the purpose of 
optimizing pattern matches.  Pattern
        matches on strings containing multiple newlines can produce confusing results 
when "$*" is 0 or undefined.
        Default is undefined.  (Mnemonic: * matches multiple things.) This variable 
influences the interpretation of
        only "^" and "$". A literal newline can be searched for even when "$* == 0".

        Use of "$*" is deprecated in modern Perl, supplanted by the "/s" and "/m" 
modifiers on pattern matching.

        Assigning a non-numerical value to "$*" triggers a warning (and makes "$*" act 
if "$* == 0"), while assigning a
        numerical value to "$*" makes that an implicit "int" is applied on the value.


> On 20010814.1156, Cory Petkovsek said ...
>
> You can also add in newlines with \n.
> 
>   perl -pi -e 's/foo/bar\nblah\n3rd line/g' <files>
> 
> However, I have not been able to search across lines
>   perl -pi -e 's/foo\nbar//g' <files>
> 
> But you can search for new lines
>   perl -pi -e 's/foo\n//g' <files>
> 
> And for dos text files, you need \r\n.
> Or to convert from dos text to unix:
>   perl -pi -e 's/\r//g' <files>
> 
> This command also works well with stdin/stdout.  And the -i (which makes the
> backup file) will do nothing (ie won't hinder) with stdin/out.
> 
> 
> -----Original Message-----
> From: Rob Hudson [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, August 14, 2001 11:51 AM
> To: EUGLUG
> Subject: [EUG-LUG:2184] Perl to the rescue
> 
> 
> I recently had the need to do some heavy search and replacement.  I
> had to replace a simple string with a huge multi-line text.  I was
> looking to sed, but perl can do this easily...
> 
> I knew perl can do search and replace from the command line via the
> following:
> 
>   perl -pi -e 's/foo/bar/g' <files>
> 
> And if you add something after the -i, it will make a copy before
> writing the new file:
> 
>   perl -p -i*.orig -e 's/foo/bar/g' <files>
> 
> This will do the same as the first example, except it will copy the
> file first as filename.orig before doing the replacement.
> 
> To take it one step further, and add in what my original question was,
> you have:
> 
>   perl -p -i*.orig -e 's/foo/`cat myfile`/eg' <files>
> 
> Here, the /e runs eval on the inside of the s///, so `cat myfile`
> actually dumps the contents of my file inside there and replaces foo
> with it.  And I tested it and if <myfile> spans multiple lines, it
> doesn't bother the perl one-liner one bit.
> 
> I haven't test this one, but it seems logical that one can do the
> following:
> 
>   perl -p -i*.orig -e 's/`cat file1`/`cat file2`/eg' <files>
> 
> To replace the contents of file1 with the contents of file2.
> 
> Mmmm.  Perl...........
> 
> 

Reply via email to