Regex escape special character in AWK if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex escape special character in AWK if statement
# 1  
Old 05-14-2012
Regex escape special character in AWK if statement

I am having issues escaping special characters in my AWK script as follows:

Code:
for id in `cat file`
do
grep $id in file2 | awk '\
    BEGIN {var=""} \
    { if ( /stringwith+'|'+'50'chars/ ) {
        echo "do this"
    } else if ( /anotherString/ ) {
        echo "do that"
    } else {
        echo "do other"
    } }'
done

I am not sure how I am supposed to escape the multitude of characters in the regex string "stringwith+'|'+'50'chars"

Last edited by Scrutinizer; 05-14-2012 at 06:25 PM.. Reason: code tags instead of quote tags
# 2  
Old 05-15-2012
Either use:

Code:
awk -vW="stringwith[+]'[|]'[+]'50'chars" 'BEGIN {var=""}
    {
      if ($0 ~ W) {
        print "do this"
    } else if ( /anotherString/ ) {
        print "do that"
    } else {
        print "do other"
    } }'

or
Code:
awk 'BEGIN {var=""}
    {
      if ( /stringwith[+]'\''[|]'\''[+]'\''50'\''chars/ ) {
        print "do this"
    } else if ( /anotherString/ ) {
        print "do that"
    } else {
        print "do other"
    } }'

# 3  
Old 05-15-2012
How about you start by telling us what is in your input file and what are you trying to get from it. It makes all the difference.
# 4  
Old 05-15-2012
This works with modern awk - POSIX character classes
Code:
awk  '/stringwith+[[:punct:]]|[[:punct:]]+[[:punct:]]50[[:punct:]]chars/' somefile

mirni is right - what are you trying to do, not how you decided to do it....
# 5  
Old 05-15-2012
I have a file that is a parsed together from several other files. Each line has a unique identifier that establishes which file it came from:

Code:
data data UID data ...

So, for each of these UID's I want to grab some value (value1). Once I find that value there are 3 potential outcomes I can obtain depending on particular Strings I find which succeed the original value:

Code:
data data UID data=data=value1...
...
data data UID data data (xyzstring)...

So, when I find xyzstring as "stringwith+'|'+'50'chars" I want to append certain text to a csv file, similarly with "anotherString", giving me the following shell script:

Code:
grep 'string' file2 | awk '{print $3}' > file
echo "this,that" > file.csv
for id in `cat file`
do
grep $id in file2 | awk '\
    BEGIN {var=""} \
    /knownString/ {split($0, tmp, "="); var=tmp[3]} \
    { if ( /stringwith+'|'+'50'chars/ ) {
        echo var",This" >> file.csv
    } else if ( /anotherString/ ) {
        echo var",That" >> file.csv
    } else {
        echo var",Other" >> file.csv
    } }'
done

I am fairly new to scripting and would be open to any suggestions.

Moderator's Comments:
Mod Comment Please use [code]...[/code] tags instead of [quote]...[/quote] tags for code and samples

Last edited by Scrutinizer; 05-15-2012 at 11:19 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to escape the @ character in an if statement check?

Hi, I am running a shell script that has to verify a password with an if statement. The password has an @ in it. I am having problems getting the if statement to test for the password. The @ causes problem. I tried the standard backslash escape but it did not work for me. How can I do it? I... (2 Replies)
Discussion started by: mojoman
2 Replies

2. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

3. Shell Programming and Scripting

Auto escape script to escape special chars in script args

This is a bit off the wall, but I often need to run scripts where there are argument values that contain special characters. For example, $ ./process.exe -t M -N -o temp.mol.s -i ../molfiles/N,N\',N\'\'-trimethylbis\(hexamethylene\)triamine.mol && sfile_space_to_tab.sh temp.mol.s temp.s It... (1 Reply)
Discussion started by: LMHmedchem
1 Replies

4. Shell Programming and Scripting

Regex within IF statement in awk

Hello to all, I have: X="string 1-" Y="-string 2" Z="string 1-20-string 2"In the position of the number 20 could be different numbers, but I'm interest only when the number is 15, 20,45 or 70. I want to include an IF within an awk code with a regex in the following way. ... (12 Replies)
Discussion started by: Ophiuchus
12 Replies

5. UNIX for Advanced & Expert Users

Escape special character

My input is: jdbc:Oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.147.109.211)(PORT=1526))(CONNECT_DAT A=(SID= MWDBD22))) In the search pattern, ( and ) and . and @ are special RE, and need to be escaped \( and \) and \. and \@ how can i do it by script or command (9 Replies)
Discussion started by: arindam guha
9 Replies

6. Shell Programming and Scripting

Escape special character

Hi, How to use * in call to pl/sql block from shell script? The line "select * from" is causing all files and directiores to show up in email notification but if I give all column names then it works, Please let me know how to use '*' instead of giving all column names, in other wirds how to... (2 Replies)
Discussion started by: sandy162
2 Replies

7. UNIX for Advanced & Expert Users

awk command in special character

Hi, I want to add below line after end of a file (i.e file1) &&echo "copy done" >> out.txt cat file1 scp user1@server1:/tmp/dir /tmp/dir1 my requirment is cat file1 scp user1@server1:/tmp/dir /tmp/dir1 &&echo "copy done" >> out.txt could any one please help me (7 Replies)
Discussion started by: anshu ranjan
7 Replies

8. Shell Programming and Scripting

awk print $1 escape all special characters

I'm using awk '{print $1}' and it works most of the time to print the contents of a mysql query loop, but occationally I get a field with some special character in it, is there a way to tell awk to ignore all special characters between my FS? I have >186K records, so building a list of ALL special... (6 Replies)
Discussion started by: unclecameron
6 Replies

9. Shell Programming and Scripting

awk escape character

ll|awk '{print "INSERT INTO SCHEMA.TABLE_NAME VALUES (`"$9 "`,"$5");" }' INSERT INTO SCHEMA.TABLE_NAME VALUES (``,); INSERT INTO SCHEMA.TABLE_NAME VALUES (`TABLE_PARTITION_Y2010M03D06.dmp`,7923328); INSERT INTO SCHEMA.TABLE_NAME VALUES (`TABLE_PARTITION_Y2010M03D06.log`,1389); But I want ' in... (2 Replies)
Discussion started by: faruque.ahmed
2 Replies

10. Shell Programming and Scripting

awk / escape character

Hi I'm trying to split a dir listing eg /home/foo1/foo2 I'm using ksh I've tried dir=/home/foo1/foo2 splitit=`echo $dir | awk -F '\/' '{print $1}'` echo $splitit nothing is output! I have checked the escape character. The only one I have found is \ BTW `pwd` | awk -F \/... (8 Replies)
Discussion started by: OFFSIHR
8 Replies
Login or Register to Ask a Question

Featured Tech Videos