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

I'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 are around 120 substitutions and around 4,600 files.

One issue: these strings are passwords, I'm hoping not to need to escape them.

So far I've only come up with:

ggrep -HF -f /home/pass "/home/find-passwords/struc/www/sites/foo.php"

which doesn't do much except list the file and matched string from pass (which contains old passwords),

no escaping is required due to the -F arg to grep.

e.g.

ggrep -HF -f /home/pass "/home/find-passwords/struc/www/sites/foo.php"

returns:

/home/find-passwords/struc/www/sites/foo.php:$dbcnx = @mysql_pconnect("blah.com", "foo", "xfgt^5");

where xfgt^5 is the match from my /home/pass file.

I'm struggling with how to replace the old password with the new, and thinking loops with a bunch of ifs might work? Or a lookup table?

I can put the old and new passwords in a file with corresponding columns.

It would be nice to be able to run in a manner that shows what it will do, and then one that does it.

Thanks for any help,

Bill

If working with files that are ":" field delimited:

 
pwds file (pwd:replace pwd):
s/1zg6/2RWAUY,2/lR:@#xyyyy$@#!$@#
JQy74TuvHyqMo:@#$@#!xxxxx$@#
32oVGmESDPBLk:@#$@234234wa#!$@#
VkrDf0FVYbg7.,2/cW:@#$sss@#!$@#
hWTRS2IF9EOQc,2/mW:@#$@aaaa#!$@#
OrfQo.EYJo9q6:@#$35345@#!$@#
2oK927ArLTgnY,B.mW:@#$@23234#!$@#

somefile:

user:s/1zg6/2RWAUY,2/lR:user:::::
user:JQy74TuvHyqMo:user:::::
user:32oVGmESDPBLk:1162:user:::::
user:VkrDf0FVYbg7.,2/cW:user:::::
user:hWTRS2IF9EOQc,2/mW:user:::::
user:OrfQo.EYJo9q6:user:::::
user:2oK927ArLTgnY,B.mW:user:::::

using awk:

 
awk -F: '
NR==FNR {pw[$1]=$2; next;}
{ FS=":";
  for (i=1; i<=NF; i++) {
    if (length(pw[$i])>0) $i=pw[$i];
  }
  print;
}
' OFS=: pwds somefile

output:

 
user:@#xyyyy$@#!$@#:user:::::
user:@#$@#!xxxxx$@#:user:::::
user:@#$@234234wa#!$@#:1162:user:::::
user:@#$sss@#!$@#:user:::::
user:@#$@aaaa#!$@#:user:::::
user:@#$35345@#!$@#:user:::::
user:@#$@23234#!$@#:user:::::
1 Like

Thanks rdtx1,

This looks promising, except the files I need to work on (e.g. somefile) are not delimited, they are free form database configuration files (see what my ggrep returns on an example file).

I can make my own pwds file (with my own delimiter in case : is used in a password) - but unless my somefile files are delimited (and they are not, they are free form), this doesn't appear to work for me.

Regards,
Bill