Help with awk script (syntax error in regular expression)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk script (syntax error in regular expression)
# 1  
Old 10-18-2012
Power 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.

Code:
#!/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[i]) }
       }
       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:

Quote:

awk: syntax error in regular expression !@#$%))*#$%^ at )*#$%^

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
# 2  
Old 10-18-2012
try replacing line:
Code:
if( $0 ~ i ){ gsub(i,s[i]) }

with
Code:
for (f=1; f<=NF; f++) { if ($(f) == i) $(f) = s[i]; }

# 3  
Old 10-18-2012
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
# 4  
Old 10-18-2012
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
# 5  
Old 10-18-2012
Escape

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:
Code:
12@!#$%^,ggg551033

where comma is the delimiter.

A found file might have this line:
Code:
dbpass = '12@!#$%^'

I'd like the program to change that to:
Code:
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):
Code:
12@\!\#\$%\^,ggg551033


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

Last edited by Franklin52; 10-19-2012 at 03:28 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

awk regular expression

Hello, I have big files which I wanna filter them based on first column. first column should be one of these strings: chr2L || chr2R || chr3L || chr3R || chr4 || chrX and something like chr2Lh or chrY or chrM3L is not accepted. I used the following command: awk '{ if ($1=="chr2L" ||... (5 Replies)
Discussion started by: @man
5 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

5. Shell Programming and Scripting

AWK script issue for the part regular expression

Hi I am having a file as shown below FILE 1 TXDD00, TXDD01, TXDD02, TXDD03, TXDD04, TXDD05, TXDD06, TXDD07, TXDD08, TXDD09, TXDD10, TXDD11, TXDD12, TXDD13, TXDD14, TXDD15, TXDD16, TXDD17, TXDD18, TXDD19, TXDDCLK, TXDJTAGAMPL0, TXDJTAGAMPL1,... (3 Replies)
Discussion started by: jaita
3 Replies

6. UNIX for Dummies Questions & Answers

Help | Unix | grep | regular expression | backreference | Syntax/Logic

Hello, I'm working on learning regular expressions and what I can do with them. I'm using unix to and its programs to experiment and learn what my limitations are with them. I'm working on duplicating the regular expression: ^(.*)(\r?\n\1)+$ This is supposed to delete duplicate lines... (2 Replies)
Discussion started by: MykC
2 Replies

7. UNIX for Dummies Questions & Answers

Syntax Help | unix | grep | regular expression | repetition

Hello, This is my first post so, Hello World! Anyways, I'm learning how to use unix and its quickly become apparent that a strong foundation in regular expressions will make things easier. I'm not sure if my syntax is messing things up or my logic is messing things up. ps -e | grep... (4 Replies)
Discussion started by: MykC
4 Replies

8. Shell Programming and Scripting

Regular expression in AWK

Hello world, I was wondering if there is a nicer way to write the following code (in AWK): awk ' FNR==NR&&$1~/^m$/{tok1=1} FNR==NR&&$1~/^m10$/{tok1=1} ' my_file In fact, it looks for m2, m4, m6, m8 and m10 and then return a positive flag. The problem is how to define 10 thanks... (3 Replies)
Discussion started by: jolecanard
3 Replies

9. UNIX for Advanced & Expert Users

Regular Expression Error in AWK

I have a file "fwcsales_filenames.txt" which has a list of file names that are supposed to be copied to another directory. In addition to that, I am trying to extract the date part and write to the log. I am getting the regular expression error when trying to strip the date part using the "ll"... (1 Reply)
Discussion started by: madhunk
1 Replies

10. Shell Programming and Scripting

awk and regular expression

Ive got a file with words and also numbers. Bla BLA 10 10 11 29 12 89 13 35 And i need to change "10,29,89,25" and also remove anything that contains actually words... (4 Replies)
Discussion started by: maskot
4 Replies
Login or Register to Ask a Question