[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cobalt-developers] Re: find and replace shell script



cobalt-developers-request@xxxxxxxxxxxxxxx wrote:

Message: 1
Date: Wed, 03 Apr 2002 11:28:32 -0800
From: Bruce Timberlake <bruce.timberlake@xxxxxxx>
Subject: Re: [cobalt-developers] find and replace shell script

Matthew Nuzum wrote:

Can anyone suggest a command that I can use in a shell script that will
help me do a find and replace action on a couple of thousand XML files?

I need to replace something like http://domain.com/images  with just
/images and it can occur several times in the same file.

Unfortunately, due to the nature of XML, many of the files don't contain
a single carriage return, just one very long line.


I don't know if sed has a problem with long lines or not, but you could
try this:

cat file.xml | sed "s/http\:\/\/domain\.com\/images/\/images/g" >
newfile.xml

(All the "\" are to "escape" the regular slashes, punctuation, etc, so
they won't be interpreted by the shell...) Then newfile.xml would be the
modified file with "/images" and file.xml would still be your original
data with the "http://domain.com/images";...

HTH!

--
Bruce Timberlake
Sun Cobalt Technology Engineer
Sun Microsystems, Inc.


--__--__--

Message: 2
Date: Wed, 3 Apr 2002 21:25:21 +0200
From: Nico Meijer <nico.meijer@xxxxxxxxx>
Subject: Re: [cobalt-developers] find and replace shell script

Hi Matt,

Can anyone suggest a command that I can use in a shell script that will
help me do a find and replace action on a couple of thousand XML files?


Try `sed`. Be sure to pipe each parsed file into a new file and then rename the newly created file to the original filename or your files will be lost.

Make backups first... Nico


--__--__--


This might also help, done starting at the root of where the files may be expected to occur: find . -type f -exec perl -p -i.bak -e 's!http://(.*\.?)domain.com/images!/images!g' {} \;

This is a find(1L) command, looking for normal files, and on each executing (-exec) the perl(1) one-liner (by way of the -e) to edit the file given to the exec function (via the {} ) in-place (-i), making a backup (with the extension .bak), looking thru the file (via the -p), executing a substitution (s) for all instances on the line (g), using the '!' as a seperator, replacing "http://"; followed by zero or more of any character, followed by 0 or 1 '.', followed by "domain.com/images/" with the string "/images/". (Single quotes were necessary above to prevent the shell from attempting to interpret the '!'s, although another character could also be used as a seperator for the values in the substitution.)

While YMMV, I hope that helps.

-Albert C.