find and replace a search string recursively in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace a search string recursively in files
# 1  
Old 08-15-2008
MySQL find and replace a search string recursively in files

Hi ,

I have a directory structure as dir and subdirectories and files under it and so on.now I need to find the files which contain the search string under every dir and subdir and replace .

my search string is like

searchstring=/a/b

string to be replaced=/a/c/b

please help.

many thanks,
mohan
# 2  
Old 08-15-2008
i think this will do
sed 's/\/a\/b/\/a\/c\/b/g' filename
# 3  
Old 08-15-2008
make sure your files are in source control before trying this!

sed -i 's/from/to/g' `find . -name \*.ext`
# 4  
Old 08-15-2008
MySQL continued

hi,

thanks for the replies. i have used them but not working..
see the code i have used :

echo "Enter the FullPackage DIR path[/eur/development ......] "
read PKG_DIR

echo " Enter the Source Pattern to be replaced "
read SS

echo " Enter the Target Pattern "
read TS

list=`find $PKG_DIR -type f`

for file in ${list}
do

#in the below command colon is a separator
sed 's:"${SS}":"${TS}":g' ${file} > /tmp/temp.txt

mv /tmp/temp.txt ${file}
done

what is wrong with my code ?

thanks for the help
# 5  
Old 08-15-2008
Use double quotes instead of single quotes in your sed statement, the single quotes prevent the shell to expand the variables:

Code:
sed "s:"${SS}":"${TS}":g" ${file} > /tmp/temp.txt

# 6  
Old 08-19-2008
thanks

hi franky,

thanks a lot for the help. working fine..

thanks
mohan
# 7  
Old 01-16-2009
MySQL

Quote:
Originally Posted by sweavo
make sure your files are in source control before trying this!

sed -i 's/from/to/g' `find . -name \*.ext`
Cheers, this does work fine as long as the filenames do not contain spaces!

How can I adapt it to handle spaces in filenames correctly?

H.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

Hi All, I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this. find / -name... (3 Replies)
Discussion started by: bhas85
3 Replies

2. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

3. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

4. Shell Programming and Scripting

How to find and replace a string with spaces and / recursively?

Hi all, I wanted to find and replace an email id from entire directory structure on a Linux server. I found that find . -type f -print0 | xargs -0 sed -i 's/abc@yahoo.com/xyz@gmail.com/g' would do it perfectly. But my search criteria has extended and now I want to search for a string1 like... (2 Replies)
Discussion started by: pat_pramod
2 Replies

5. 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

6. UNIX for Advanced & Expert Users

Recursively search the string from a column in no. of files

i have a file named keyword.csv(contains around 8k records) which contains a no. of columns. The 5th column contains all the keywords. I want to recursively search these keywords in all .pl files(around 1k) and display the filename....Afterthat i will use the filename and some of the column from... (3 Replies)
Discussion started by: millan
3 Replies

7. 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

8. Shell Programming and Scripting

Search and replace string in files

I'm trying to remove the following string from several files. <img heigth="1" width="1" border="0" src="http://myteenmovies.net/t.php?id=5540372">I'm using the following script #!/bin/bash # This script will search and replace all regular files for a string # supplied by the user and... (1 Reply)
Discussion started by: d13g0sv
1 Replies

9. Shell Programming and Scripting

sed search and replace recursively

Hi, I tried making a shell script which recursively search in a given directory for all files *.txt and then search and replace some text and after that save each file as $filename.dynamips.txt I would like to get this done with sed. I tried but couldn't get it to work. BTW this is not... (1 Reply)
Discussion started by: 2bone
1 Replies

10. Shell Programming and Scripting

Replace all occurances of a string in all file-/foldernames, recursively

I need a script that will replace all occurances of a string in all filenames and foldernames, recursively. Right now I have this script: for f in `find -name *eye*`; do echo processing $f g=`expr "xxx$f" : 'xxx\(.*\)' | tr 'eye' 'm'` mv "$f" "$g" done The problem is that tr... (2 Replies)
Discussion started by: TheMJ
2 Replies
Login or Register to Ask a Question