Writing script for finding a string and replacing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing script for finding a string and replacing
# 1  
Old 08-06-2008
Error Writing script for finding a string and replacing

Hi all

i need some help in writing a small script that searches a string and then replaces it by a new string
for searching the best result i get is from find comand combined with xargs
e.g.
find . -name "*.*" |xargs grep -l "search string"
this command I use in root directory and this gets me all the file in that directory and sub directories that has that string
I want to replace that string with new string in the files that gets as output from the find command.
# 2  
Old 08-06-2008
Code:
find . -name '*' -type f -exec grep -l 'pattern' {} \; | \
while read file
do
      sed 's/pattern/newpattern/g' $file > ./tmp.tmp
      mv ./tmp.tmp $file
done

is one way to do it.

This is a perfectly horrible idea. Unless you like rebuilding your system.

A global search can change system files and protections on those files, for example. In other words, it is likely that your system will not reboot correctly, depending on what string/file you are changing. Limit the search with -user <some username not root or bin>.
# 3  
Old 08-06-2008
Thanks for help
for your doubt regarding system files getting changed....
i will be running it from a specified location and "." specifies present location and sub directories inside the directory.
Also i will be modifying it to look for .pl files

I tested the script also and it works!!!! Smilie
# 4  
Old 08-06-2008
find . -name "*.*" |xargs sed -i 's/string/string-to-replace/'
# 5  
Old 08-06-2008
Error I concur with Jim-M about filesystem corruption

Would recommend not relying on the . and your current location. Imagine the script finding its way to /etc/passwd with your instruction to change 'root' to 'branch' (I am just making this up as I type) -- think you will be able to login again? If you 'know' the location, account for it so as to prevent future troubles.

Probably should not be searching for *.* -- perhaps *.txt or similar to minimize chance of changing something.

And this comes from someone who "thought he programmed for everything" until something went awry and I changed the permissions on every file in my system (old job) with a run-away find command. We weren't live yet (still loading files), so I could restore a backup and correct my errant script.

Lastly, using the find in this manner could mean exceptionally long execution times.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding a format in a file and replacing it

Hi, I need help in the following: I have a file in directory with mutiple comma seperated values. One of the value is a date and time format like 2012-04-10 xx:yy:zz I need to find that time format in the file and then replace it with xx:yy+1:zz and then save it as a new file and copy it to a... (3 Replies)
Discussion started by: rabh
3 Replies

2. Shell Programming and Scripting

Shell Script [Replacing string]

My friend and I are working on a project together and we are trying to create a shell script that renames, replaces a string in a text file. The newly created file with the substitution would replace the name of the original file. And the original file would be saved as "____.txt.bak" This is... (6 Replies)
Discussion started by: jzhang172
6 Replies

3. Homework & Coursework Questions

Finding/replacing text and redirection help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: What command would rename "sequentialInsert", in ~cs252/Assignments/commandsAsst/project/arrayops.h, to... (2 Replies)
Discussion started by: lothwen
2 Replies

4. Shell Programming and Scripting

Finding a String and Replacing it with an incremental value

Hi Friends, I have a text file which has about 200,000 records in it. we have a string which repeats in each and every record. So we have to write a script in ksh which finds that string on each line and replaces it with a new string(incremental value) for a set every four records. To be... (12 Replies)
Discussion started by: vpv0002
12 Replies

5. Shell Programming and Scripting

Finding and replacing links

Hi, I would like to do a scripting for finding the links based on the name I have and replace the links with the new name. General find command lists everything for that links ( means all the sub-sirs and all the files), i need only the main link and replace. Can you anyone give me some... (1 Reply)
Discussion started by: rrb2009
1 Replies

6. Shell Programming and Scripting

Need help in writing script for finding files in the unix machine?

I would like to find whether a file exists in the UNIX machine. That i can check using if ;then echo "exists" echo " `cat $file` " else echo "invalid file" fi. and i can find out using : find / -name "filename" . But it i have wanted to search in all directories. How to get... (3 Replies)
Discussion started by: rparsa001
3 Replies

7. Shell Programming and Scripting

Need help in finding and replacing port numbers.

Hi All, I am trying to write a shell script which firstly will search some files and then increase the port numbers mentioned in them by a certain no. let me clear it with an example- suppose there r few files a,b,c,d.... file a's content- <serverEntries xmi:id="ServerEntry_1"... (3 Replies)
Discussion started by: ankushsingh10
3 Replies

8. Shell Programming and Scripting

need help in finding a string and to send an email using shell script

Hi All i am writing a shell script which will search for a string "expires". once the search string is found it has to give the email address as the output and send an email to the person This is basically to find the encrypetd keys which are loaded in the unix server Below are sample... (10 Replies)
Discussion started by: ranga27
10 Replies

9. Shell Programming and Scripting

help for shell script of finding shortest substring from given string by user

please give me proper solution for finding a shortest substring from given string if string itself and first char and last char of that substr are also given by user if S="dpoaoqooroo" and FC="o" and LC="o",then shortest substr is "oo" and rest of the string is "dpoaoqroo" i have code but it is... (1 Reply)
Discussion started by: pankajd
1 Replies
Login or Register to Ask a Question