Solved

SED Mass find and replace

Posted on 2004-08-03
4
1,777 Views
Last Modified: 2012-05-05
I'm trying to do a mass find and replace using sed. I'm not very good with sed so I don't know how to make it find "cpdigest.com" and replace it with "connectpress.com" in a few thousand files.

Here's what I have so far: sed -e "s/\cpdigest.com/connectpress.com/g

But I dont know where to go from here.

Thanks
0
Comment
Question by:wgordy
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
Gns earned 500 total points
ID: 11707261
You could wrap it up in a for-loop, or just use the -i option.... This is from the manpage:
       -i[suffix], --in-place[=suffix]
              edit files in place (makes backup if extension supplied)
-------
So you could for example do
sed -i -e 's/cpdigest.com/connectpress.com/g' filenames
or
sed --in-place=".bup" -e 's/cpdigest.com/connectpress.com/g' filenames
...
Perhaps safest to test with one or two files to see that it really does what you expect, and then using the backup suffix method when you do 'em all... provided you have the space (of course:-).

-- Glenn
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 11708467
Just a note... many versions of sed do not have -i.   I believe it appears as a GNU extension in more recent versions only... If you find yourself on a system with a different sed implementation that doesn't have the -i option, and still want a one-line command, you can try perl, also  

I.E.  The perl way to do this would be to issue the command:  perl -i'.bak' -pe 's/cpdigest\.com/connectpress.com/g' *
(In that case you have a perl regular expression instead of a POSIX regular expression, also)

If you want to do the replacement recursively, you need a shell script.
Here's one way:

#!/bin/sh

for i in `find /path/to/start/from -type f` ; do
       mv $i $i.tmp$$
       sed -e 's/cpdigest\.com/connectpress.com/g' < $i.tmp$$ > $i
       mv $i.tmp$$ $i.bak
done


You can also use find options like   -name \*.txt

for example, if you only want to alter files that contain that pattern in the filename
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 11712918
Hi wgordy,
go for Gns solution but i would use find instead of a for loop(for loops always has problem with strange file names etc.), like this:
find /path/to/files -type f -exec sed -i -e 's/cpdigest.com/connectpress.com/g' {} \;
but this is kind of dangerous so test with this first:
find /path/to/files -type f -exec sed -e 's/cpdigest.com/connectpress.com/g' {} > {}.kalleballe \;
this way you ca take a look at some files named *.kalleballe and see if it worked, and then use the command line above.

/Rob
0
 
LVL 20

Expert Comment

by:Gns
ID: 11713979
Well Mysidia, this is the Linux TA... It's one of the very few places where we can safely rely on the GNU-ish features being there;-).
And sure, you need a version 4 sed... But this isn't _that_ brand new exactly. Most modern distros will have moved to this version by now.
Further, if the feature showed to be lacking, I was planning to show some further examples along the same vein you and Rob did... (You just beat me to it (I'm much for dialog with the questioner;)...:-) Anyway... Thanks for doing them!

Oh, Rob, minor point... When we're talking thousands of files I tend to prefer using the xargs strategy WRT find -exec, simply to reduce the number of fork/execs that need be done ... so that transforms your first example to
find /path/to/files -type f -print | xargs sed -i -e 's/cpdigest.com/connectpress.com/g'
... Your second example doesn't lend it self to that though (for obvious reasons:-) but is fairly close to the
find /path/to/files -type f -print | xargs sed --in-place=.bup -e 's/cpdigest.com/connectpress.com/g'
which is inverse and fairly easy to "undo" (execise left to the reader:-):-)

-- Glenn
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run same command on multiple files in Linux 3 55
ignore other .htaccess 2 58
Bash script - Exit out of choice loop 2 43
Trying to install php56 on CentOS 7 get GPG error 5 27
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question