Come on guys - this isn't really an RTFM task!
On Mon, May 13, 2002 at 11:09:53PM +0100, Paul wrote:
Hi Folks
I am in need of some help - I have a large number of small text files that need to be scanned and one specific phrase replaced at each occurrence excluding the first line (which may or may not contain the offending words). I suspect that something like perl may be the most suitable tool for the task in hand. But I know nothing about it.... Just to add to the problem, the files are scattered across multiple directories and the key phrase may span two lines.
A crude way of replacing "I use Windows" with "I now use Linux" over multilines would be:
find . -type f -exec perl -00pi -e 's/I\s+use\s+Windows/I now use Linux/msg' {} ;
However, this wouldn't solve the problem of it appearing on the first line. To do that, I'd probably do a script like this:
foo.pl:
--------------------
#!/usr/bin/perl -w $_=<>; # get first line print; # print out unchanged undef $/; # undef eol char so all file gets read in $_=<>; # read file s/I\s+use\s+Windows/I now use Linux/msg; # do the substitution print; # print out the results.
---------------------
Then do:
find . -type f -exec perl -i foo.pl {} ;
Note that:
- This won't find things like:
I u se Windo ws
And it will replace:
I use Windows
with:
I now use Linux (all one line)
Hope this helps.