Help with search & replace
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. Regards, Paul.
RTFM man sed On 13-May-2002 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.
Regards, Paul.
_______________________________________________ main@lists.alug.org.uk http://www.anglian.lug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
-- Intelligent: someone who agrees with our opinions. Ambrose Bierce. Raphael Mankin E-Mail: raph@panache.demon.co.uk ----------------------------------
This is a typical example of the most irritating reply .... Sorry but I felt I should voice my opinion in this free and "helpful" community...... Simon P.S. Flame retardent suit on !!! On Tuesday 14 May 2002 10:08 pm, Raphael Mankin wrote:
RTFM
man sed
<snip>
Simon <simon@sparksy.org.uk> wrote:
This is a typical example of the most irritating reply ....
At least he didn't just post "STFW", as I'm sure this sort of problem is dealt with in quite a few FAQs, including the sed one. It's multiline, so I'd probably use a scripting language with regular expression support and find the multiline flags. perl, python and guile are installed on quite a few and I quite like the programmable richness of guile's regexp-substitute/global (I think that's the function), although its quoting rules trip me up, as it's not perl-like. Then again, it looks like perl6's regexps may not be perl-like either...
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.
On Fri, 2002-05-17 at 09:00, Chris Allen wrote:
And it will replace: I use Windows
with: I now use Linux (all one line)
If you use the RE: s/I(\s+)use(\s+)Windows/I\1now use\2Linux/g then the spacing between 'I' and 'use' will be put between 'I' and 'now use'. Similarly for the next bit. Alexis
Alexis Lee <memehack@btopenworld.com> wrote:
If you use the RE: s/I(\s+)use(\s+)Windows/I\1now use\2Linux/g then the spacing between 'I' and 'use' will be put between 'I' and 'now use'. Similarly for the next bit.
Maybe even pipe it all through fmt to fix the word wrap for the new lengths?
participants (6)
-
Alexis Lee -
Chris Allen -
MJ Ray -
Paul -
Raphael Mankin -
Simon