Global search and replace across multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Global search and replace across multiple files
# 1  
Old 11-02-2010
Global search and replace across multiple files

Hi all

I'm in need of a command which can replace a specified string with another string - across multiple files within multiple sub-directories (I intend to run it from / )

I've used the following to get a list of the files:

Code:
find . | xargs grep <string1>

But that's as far as I've got. I've though about using sed but not sure what route to take.

Any suggestion greatly appreciated.

TIA.
# 2  
Old 11-02-2010
If your sed allows the -i switch, you could do something like this:
Code:
find . -type f| xargs sed -i 's/old/new/g'

perl has a switch to edit a file in place too. If you have neither, you could use temporary file names and mv the changed file back to the name of the original.

Last edited by zaxxon; 11-02-2010 at 12:18 PM.. Reason: added a missing /g
# 3  
Old 11-02-2010
I am not sure if it can be done using some single command. I have tried writing a small ksh shell script. Hope this will help.
Code:
#/bin/ksh
for k in `find ./ -type f| xargs grep oldstring
do
   filename=`echo $k | cut -d ":" -f1`
   sed 's/oldstring/newstring/g' $filename >${filename}.bk
   mv ${filename}.bk ${filename}
done


Last edited by Franklin52; 11-02-2010 at 10:11 AM.. Reason: Please use code tags, thank you.
# 4  
Old 11-03-2010
Thanks Sanjay. I tried your example but I keep on getting an error saying that my $ORACLE_HOME isn't set (this is an Oracle db and app server).

Weird thing is that I'm executing this as root from / so why is it asking for a variable to be defined?!
# 5  
Old 11-03-2010
There are a number of problems with sanjaypraj's script:

Code:
#/bin/ksh             # Missing ! after #
for k in `find ./ -type f| xargs grep oldstring  # No closing backquote, and expect an Arg list too long error
do
   filename=`echo $k | cut -d ":" -f1`  # not sure why this is needed
   sed 's/oldstring/newstring/g' $filename >${filename}.bk
   mv ${filename}.bk ${filename}  # This is dangerous
done

Code:
find . -type -f | while read FILE; do
  sed "s/oldstring/newstring/g" $FILE > $FILE.new
  cp -f $FILE.new $FILE && rm $FILE.new
done

Code:
# This is dangerous.  Suppose your "sed" updates an important system file...
$ ls -l rc.tcpip
-rwxrwxr--    1 root     system         6610 Oct 23 09:33 rc.tcpip
 
$ sed "s/old/new/g" rc.tcpip > rc.tcpip.new
 
$ ls -l rc.tcpip.new
-rw-r--r--    1 root     system         6610 Nov 3 10:49 rc.tcpip.new
 
$ mv rc.tcpip.new rc.tcpip
 
$ ls -l rc.tcpip
-rw-r--r--    1 root     system         6610 Nov 3 10:49 rc.tcpip

And when you reboot three months later....

Last edited by Scott; 11-03-2010 at 07:06 AM.. Reason: cut & paste error; typo
# 6  
Old 11-03-2010
Hi Scottn,
Thanks for reviewing my code and finding the missing stuff, one of which probably because of a copy and paste error (`).
The reason for using
Code:
filename=`echo $k | cut -d ":" -f1`

is to cut the filename from the list

If you execute the find command the output i am getting
Code:
==> find . -type f | xargs grep hello 
./dir1/file1:hello
./dir1/file2:hello
./dir1/dir2/file4:hello
./dir1/dir2/file5:hello

# 7  
Old 11-03-2010
Quote:
Originally Posted by sanjaypraj
Hi Scottn,
Thanks for reviewing my code and finding the missing stuff, one of which probably because of a copy and paste error (`).
The reason for using
Code:
filename=`echo $k | cut -d ":" -f1`

is to cut the filename from the list

If you execute the find command the output i am getting
Code:
==> find . -type f | xargs grep hello 
./dir1/file1:hello
./dir1/file2:hello
./dir1/dir2/file4:hello
./dir1/dir2/file5:hello

Ah, of course (why using cut Smilie). I wasn't using grep, so didn't see that.

Using grep -l would remove the need for cut too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search/Replace in multiple files recursively

Hi there, I am using AIX and trying to search and replace a string with another string in multiple files in different directories. I wanted to search replace in steps so I don't change all of the instance anywhere in the server at once, minimizing impact. STEP 1: -------- I first searched... (5 Replies)
Discussion started by: zaino22
5 Replies

2. Shell Programming and Scripting

Search and Replace in multiple files

Hello, I have hundreds of files in which I need to change email address. Here is what I am trying to do: 1. All text files are in a directory "a" 2. In the text file, I want to replace email address for preparer. All these lines start with {{PreparerEmail and end with }}. The email... (3 Replies)
Discussion started by: cartrider
3 Replies

3. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

4. Shell Programming and Scripting

perl: search replace in multiple files

When I use special characters the command to replace multiple files with a string pattern does nt work. ---------- Post updated at 12:33 PM ---------- Previous update was at 11:38 AM ---------- This works perl -pi -e 's/100/test/g' * This does nt work perl -pi -e 's... (1 Reply)
Discussion started by: w020637
1 Replies

5. Shell Programming and Scripting

String search and replace in multiple files.

Hello. I have five config files in /etc that I want to edit in one click for testing. I would like to make a script like this : #!/bin/bash # a_file="/etc/file_1" src_str="src_string_1" rpl_str="rpl_string_1" calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

7. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

8. Shell Programming and Scripting

Perl: Global Search and replace

I have a file where the rows correspond to individuals and the columns are about 106 variables. Each variable is coded as either ACGT, and "missing" is coded as blank. This is a tab delimited file. I'm trying to replace all blanks (" ") with 0. The simple script I have is only replacing some of the... (3 Replies)
Discussion started by: epi8
3 Replies

9. Shell Programming and Scripting

Need to search and replace in multiple files in directory hierarchy

Hello all I need to search and replace in multiple files that are in directory hierarchy Im using the : find . -name "*.dsp" -print | xargs grep -n -o Test.lib" , I like to be able to replace every instance of Test.lib with empty space . how can I write one liner that does this ? (3 Replies)
Discussion started by: umen
3 Replies

10. UNIX for Dummies Questions & Answers

Global search ok...but replace?

Gurus, I have in my /tmp directory 26 files "filea", "fileb"..."filez". Each file contains the name of a database 'dwora' at many, many places within each file. My boss decided to change the name of the db so I need to do (what i'd call) a global search&replace of that string in all my... (0 Replies)
Discussion started by: alan
0 Replies
Login or Register to Ask a Question