[Fwd: Re: Search & Replace Issue]





--- Begin Message --- On Sun, 2006-12-24 at 12:13 -0800, Garrett Cooper wrote:
Jack Stone wrote:



From: Parv <parv@xxxxxxxx>
To: Josh Paetzel <josh@xxxxxxxxx>
CC: Jack Stone <antennex@xxxxxxxxxxx>, freebsd-questions@xxxxxxxxxxx
Subject: Re: Search & Replace Issue
Date: Sun, 24 Dec 2006 02:56:32 -0500

in message <200612232230.58352.josh@xxxxxxxxx>,
wrote Josh Paetzel thusly...

On Saturday 23 December 2006 21:29, Jack Stone wrote:
Appreciate a tip on how to search & replace hundreds of *.htm
files:
From this:

<li><a href="http://www.domain.com/tales/wouf.html
To this:
<li><a href="tales/wouf.html


perl -p0777i -e 's/http:\/\/www.domain.com\///g' *.htm

Is -0777 really necessary (causes whole file to be stored in
memory)? But that is not really the point of this reply.

Above is a fine opportunity to use alternative delimiters (and to
restrict the matching (only to link URLs)) ...

perl -pi -e 's!(?<=href=")\Qhttp://www.domain.com!!g' *.html


... in case of "hundreds of *.htm", use xargs(1) pipeline ...

find dir-of-HTML-files -type f -name '*.html' -print0 \
| xargs -0 perl -pi -e 's!(?<=href=")\Qhttp://www.domain.com!!g'


Feel free to change Perl version with sed (the version of sed with
-i option[0]) one ...

find ... \
| ... sed -i -e 's,\(href="\)http://www\.domain\.com,\1,g'


[0] That makes this reply on point.


- Parv


Parv and all:
Many thanks for these various tips and your time to make them!

I usually use sed(1) myself, but for the life of me, I could not find
a way to properly apply delimiters or syntax to get it to work. I was
close, but no cigar! Too many slashes and commas I guess.

Such a "tool" will indeed be a giant timesaver!

Merry Xmas!

All the best,
Jack

One thing with regular expressions though, is that you can control
the command characters to use with defining the search and replace
keywords and replacements. If you see my example, I used pipes because
you had a number of forward slashes (/), so it allows you to cut down on
the number of escaping backslashes in your regular expression / replacement.
Cheers and a Merry Christmas to you too!
-Garrett
_______________________________________________
freebsd-questions@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@xxxxxxxxxxx"

The -i option to sed enables it to rewrite a file in place, removing the
need to create new files, delete the old ones, and rename the new ones.
But it needs careful testing, and should never be used without a good
backup of all the files that it might touch. Powerful tools are often
dangerous!

--- End Message ---
_______________________________________________
freebsd-questions@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@xxxxxxxxxxx"

Relevant Pages

  • Re: Search & Replace Issue
    ... Above is a fine opportunity to use alternative delimiters (and to ... One thing with regular expressions though, is that you can control the command characters to use with defining the search and replace keywords and replacements. ... If you see my example, I used pipes because you had a number of forward slashes, so it allows you to cut down on the number of escaping backslashes in your regular expression / replacement. ...
    (freebsd-questions)
  • Re: Regex question (easy?)
    ... thanks for the advice Jon. ... I'm essentially building an rtf file from a text file (generated by ... the replacements using the Groups within that Match Collection. ... for people who don't use regular expressions very often. ...
    (microsoft.public.dotnet.general)
  • Re: RegExp to match against RegExps
    ... If all you want to do is match something between slashes, ... Verifying that something between slashes is a well-formed regular ... regular expressions have been beefed up enough to make it possible. ... Determining, for a given place in a Perl program text, whether a slash ...
    (comp.compilers)
  • Re: preg_match error
    ... Perl-Compatible regular expressions have two components: ... some optional modifiers. ... The pattern is enclosed by slashes and the ... This is practical when the pattern itself contains slashes. ...
    (comp.lang.php)
  • Re: more on unescaping escapes
    ... andrew cooke wrote: ... it's intended for regular expressions, but would simplify things ...
    (comp.lang.python)