Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to find and replace (sub) a pattern match in multiple files? 3

Status
Not open for further replies.

QueenAnne

Programmer
May 21, 2004
3
US
I need to change a pattern match on multiple lines in multiple files. How do I do this?
Say for example: Files a.java, b.java, c.java... have hard coded passwords that need to be changed and the password could be on multiple lines not within a set column. Thanks very much for any assistance.
 
This is untested, but may get you started.
{
if (FNR==1){
if (fn) close(fn)
fn = "new" FILENAME
}
gsub("oldpwd","newpwd")
print>fn
}

CaKiwi
 
ok,
Thank you CaKiwi. I found a way to do this using sed and it works for the most part. The only problem I have now is that sed is matching too much.

for FILE in *java
do
cat $FILE sed 's/devPassword/prodPassword/' > tmpFile
cp tmpFile $FILE
done
rm tmpFile

this will match devPassword, devPassword2, devPassword32323
this is not specific enough, I want to change only the patterns matching "devPassword".
I have tried 's/\<devPassword\>/prodPassword/', but it does not work.
 
for FILE in *java
do
sed 's/devPassword[ ]+/prodPassword/' $FILE > tmpFile
cp tmpFile $FILE
done
rm tmpFile

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Or perhaps

sed 's/devPassword\([ ]+\)/prodPassword\1/' $FILE > tmpFile

If the password can be delimited by characters other than a space, put them in the square brackets.

CaKiwi
 
If the password is a literal string surrounded by doublequotes, you may consider something like this:
for f in *.java; do
cp $f $f.bak
sed 's!"oldPassword"!"newPassword"!g' $f.bak >$f
done

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
thanks all,
i'll give these additional tips a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top