how to get list of files changed by sed while replacing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get list of files changed by sed while replacing
# 1  
Old 05-24-2011
how to get list of files changed by sed while replacing

Hi

We have a shell script to find a string in a large set of files and replace it using the sed command.in the same time we also want to store the list of files it checked.
***
The existing script is as below

Code:
#!/bin/bash
#1
_r1="inst_group=ems7770"
_r2="inst_group=dba"
# Escape path for sed command
r1="${_r1//\//\\/}"
r2="${_r2//\//\\/}"
echo "Listing all files.."
# List all files having _r1
find . -name "*" -type f | xargs grep -os ${r1} > path_5_all_log
echo "Replace started.."
# Replace _r1 in all files with _r2
find . -name "*" -type f -exec sed -i "s/${r1}/${r2}/g" {} \;
echo "Replace over.."

****

Here we are running the find two times, one to get the details of files being replaced.Since the folders are very big, it is taking lot of time.
Is there any way we can use

this '
Code:
find . -name "*" -type f -exec sed -i "s/${r1}/${r2}/g" {} \;

' line to also redirect the list of files to a file, so that we can avoid using the other find statement?

thanks
Sabkan

Last edited by pludi; 05-24-2011 at 04:35 AM..
# 2  
Old 05-24-2011
You can speed up a bit that command by getting rid of exec and just invoke sed once for many files:
Code:
find . -type f | xargs sed -i "s/${r1}/${r2}/g"

Then to split the output of find, use tee(1):
Code:
find . -type f | tee listOfFiles.lst | xargs sed -i "s/${r1}/${r2}/g"

This User Gave Thanks to mirni For This Post:
# 3  
Old 05-24-2011
You could do something like..

Code:
find . -name "*" -type f -exec grep -l ${r1} {} \; | while read FILE
do
echo $FILE
sed -i "s/${r1}/${r2}/g" $FILE
done

check find output for absolute or relative path if required.
# 4  
Old 05-24-2011
Hi

Thanks Anchal/Mirni for your inputs.

Can we include grep in this command?
Code:
find . -type f | tee listOfFiles.lst | xargs sed -i "s/${r1}/${r2}/g"

so that in listOfFiles.lst we get only the files containing the search patterns?

thanks
Sabkan

Last edited by Franklin52; 05-24-2011 at 12:57 PM.. Reason: Please use code tags
# 5  
Old 05-24-2011
Code:
find . -type f | xargs grep -l "$r1" |tee listOfFiles.lst | xargs sed -i "s/${r1}/${r2}/g"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

Bring back a file changed with sed

Hello everbody I changed one of my important files with a false sed statement by mistake now I lost my file and I hope I could bring it back what I did was: sed '/^..//' a > myfile myfile should have been another file like b ot something I know I also forgot to place an 's' to the... (5 Replies)
Discussion started by: miriammiriam
5 Replies

3. UNIX for Dummies Questions & Answers

Find only files that changed Yesterday

Hi , I know that find / -type f -mtime -1 will show all modified files that changed 24hrs preceeding the time i run the command. This will include list of files that changed today. How do i find only files that changed yesterday staring from 00:00hrs to 23:59? Thanks HG (2 Replies)
Discussion started by: Hari_Ganesh
2 Replies

4. Solaris

Replacing a string in a long list of files

I have a script that needs to read a file with a long list of /path/filenames - replace the name of the server in each file - and write the file to the same path with a date extension. This is the script that I have so far #!/bin/ksh umask 022 LIST=`scripts.list` for i in $LIST do ... (2 Replies)
Discussion started by: bjdamon
2 Replies

5. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

6. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

7. Shell Programming and Scripting

Replacing or removing a long list of pattern by using awk or sed

Input: >abc|123456|def|EXIT| >abc|203456|def|EXIT2| >abc|234056|def|EXIT3| >abc|340056|def|EXIT4| >abc|456000|def|EXIT5| . . . Output: def|EXIT| def|EXIT2| def|EXIT3| def|EXIT4| def|EXIT5| . . My try code: (9 Replies)
Discussion started by: patrick87
9 Replies

8. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

9. Shell Programming and Scripting

using sed to replace a pattern in list of files

Hi All, using the below grep command,I get the following output: $grep -irl "bc" /home/applmgr/amit > file_list.log $cat file_list.log /home/applmgr/amit/xyz.log /home/applmgr/amit/abc.log Requirement ========= Need sed utility to replace "bc" with "xy" pattern in the list of files... (4 Replies)
Discussion started by: a1_win
4 Replies

10. AIX

way to copy only changed files

Hello, we are going through a restructoring of file systems and will need to copy the contents of one file system to another box, then a few days later copy it again. Is there a way on the second copy to only copy files that have changed? Thanks in advance (3 Replies)
Discussion started by: zuessh
3 Replies
Login or Register to Ask a Question