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