Help with awk script (syntax error in regular expression)

I've found this script which seems very promising to solve my issue:

To search and replace many different database passwords in many different (.php, .pl, .cgi, etc.) files across my filesystem.

The passwords may or may not be contained within quotes, single quotes, etc.


#!/bin/bash
f="passwords.csv"
find /path -type f -name "*.txt" | while read FILE 
do
    awk 'BEGIN{ FS="," }
    FNR==NR{ s[$1]=$2;  next }
    {
       for(i in s){      
        if( $0 ~ i ){ gsub(i,s) }
       }
       print $0
    }' $f $FILE  > temp
    mv temp $FILE
done

My passwords.csv file contains old passwords and new ones, comma delimited. I've altered the find command within this script to find the files I want.

The script works if the first column (old password) has no special characters, but since these are passwords, there are lots of special characters.

It seems the second column (new password) can contain special characters ...

I've tested this by having the find command (within the script) find a specific file, with a password that has no special characters, and using a test passwords.csv with the non-special password (in the first column), and the new password (in the second column) having special characters, and it does the substitution.

However, if the reverse is true (and in most cases it is), I receive the error:

Is there something I can do to the script that will treat both columns (or matches), as literals? I say matches because I'm actually not sure if it is my passwords.csv file containing the special character password, or the file that contains it, that is breaking this. Whatever it is, I'd like to have this script work.

Thanks for any assistance,

Bill

try replacing line:

if( $0 ~ i ){ gsub(i,s) }

with

for (f=1; f<=NF; f++) { if ($(f) == i) $(f) = s; }

Hi rdrtx1,

This silences the error but does not cause the script to work / substitution to occur. The old password remains and is not substituted with the new.

Thanks,
Bill

You can search for fixed string, literal matches using the index() function. You can then use the return value of index() along with the substr() and length() functions to extract the portions of the line which surround the matching text. These can then be concatenated with the replacement text to create the new line.

Obviously, it's more involved than regular expression substitions, but it's not vulnerable to metacharacters wreaking havoc.

Regards,
Alister

Thanks for the additional info, I'm not sure what to do with it unfortunately.

Is it the passwords in the passwords.csv file, or the matches of them in the found files (or both), that causes the error?

If it's the passwords in the passwords.csv file, can I escape them in some way?

My passwords.csv file looks like this:

12@!#$%^,ggg551033

where comma is the delimiter.

A found file might have this line:

dbpass = '12@!#$%^'

I'd like the program to change that to:

dbpass = 'ggg551033'

I'm open to other solutions.

Thanks,
Bill

---------- Post updated at 09:29 PM ---------- Previous update was at 09:07 PM ----------

Looks like escaping column 1 in this fashion (with back slashes):

12@\!\#\$%\^,ggg551033

might work ... will need to determine what needs escaping and what does not.