Escape ' in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Escape ' in awk
# 1  
Old 10-25-2011
Escape ' in awk

I am trying to escape single quote in awk, but could not:

Code:
awk ' BEGIN{ fstr="COMPRESS('Y','N')" } 
{
substr($1,len-3,4)~/_IND/
len=length($1)
if(substr($1,len-3,4)~/_IND/)
  printf("%-32s%-14s%-5s%-14s\n",$1,$2,$3,$fstr,$4,$5,$6,$7)
}
' tt.txt > out.log

Error:
Code:
awk: Field $(COMPRESS(Y,N)) is not correct.
 The input line number is 1. The file is tt.txt.
 The source line number is 6.


Last edited by Franklin52; 10-26-2011 at 03:33 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-25-2011
Code:
awk ' BEGIN{ fstr="'"COMPRESS('Y','N')"'" }

# 3  
Old 10-25-2011
Still getting error !

awk: Field $(COMPRESS('Y','N')) is not correct.
The input line number is 1. The file is tt.txt.
The source line number is 6.
# 4  
Old 10-25-2011
Which shell are you using? What is the version of awk? My bash (version 4.1.2) and awk (version GNU Awk 3.1.7) work smooth without spilling out errors.
# 5  
Old 10-26-2011
New to unix Smilie so if need the commands to check the shell and awk version Smilie
# 6  
Old 10-26-2011
Try this... single double single Y single double single
Code:
awk 'BEGIN{ fstr="COMPRESS('"'Y'"','"'N'"')" }'

--ahamed

---------- Post updated at 08:33 PM ---------- Previous update was at 08:31 PM ----------

Code:
awk --version

bash --version

--ahamed
# 7  
Old 10-26-2011
Typically the command awk --version will generate the version of awk that you are using. If you are running on Solaris, it's wise to use nawk instead as the default awk in that environment is quite old and has lots of limitations.

The command uname -a will generate information about the OS you are running and the version.

Running echo $SHELL at the command prompt should give you the name of the shell you are using. To get the version you might might trying running the shell name with --version on the command line; modern versions of Korn shell and bash support this.

As for your problem...
What is generating the error message is in your printf() statement. You do not need the dollar sign in front of the variable name fstr. Past that, the bit of quoting that was suggested should work.

I'm not a fan of opening and closing quotes in the middle of an awk programme as that leads to maintenance problems. If it is just one string that you need to have single quotes appear in you could try this:

Code:
awk -v fstr="COMPRESS('Y','N')" '
{
    substr($1,len-3,4)~/_IND/
    len=length($1)
    if(substr($1,len-3,4)~/_IND/)
       printf("%-32s%-14s%-5s%-14s\n",$1,$2,$3, fstr, $4,$5,$6,$7)
 }
 ' tt.txt > out.log

Supplying the variable on the awk command, outside of the programme, allows you to use the single quotes within the double quotes, and it's easier to read (IMHO).
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

String has * as the field delimiter and I need echo/awk to escape it, how?

Hi, I am trying to read an Oracle listener log file line by line and need to separate the lines into several fields. The field delimiter for the line happens to be an asterisk. I have the script below to start with but when running it, the echo command is globbing it to include other... (13 Replies)
Discussion started by: newbie_01
13 Replies

2. Shell Programming and Scripting

awk to escape space in name

I am searching a file and not able to escape a space if $i has a space in the name. I tried escaping it with a \ and also trying to add it to allow for spaces in the search. Neither are correct as the first awk only outputs path and the second awk doesn't run. Thank you :). first awk awk... (3 Replies)
Discussion started by: cmccabe
3 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 escape special character in AWK if statement

I am having issues escaping special characters in my AWK script as follows: 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 { ... (4 Replies)
Discussion started by: purebc
4 Replies

5. Shell Programming and Scripting

awk not escape my bash variable

I tried to parse data from switch configuration files vlan 1727 name SQ5506-15 by port tagged ethe 8/1 to 8/2 untagged ethe 1/13 ! vlan 2105 name SQ5620-7007(BR2) by port tagged ethe 8/1 to 8/2 untagged ethe 1/17 ! interface ethernet 1/13 port-name SQ5506-15.nic0 rate-limit... (2 Replies)
Discussion started by: winggundamth
2 Replies

6. Shell Programming and Scripting

Using awk with the date command and escape characters

I have a file that is a log file for web traffic. I would like to convert the timestamp in it to unix time or epoch time. I am using the date command in conjunction with awk to try to do this. Just myfile: 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813 28/Aug/1995:00:00:38 1... (3 Replies)
Discussion started by: jontjioe
3 Replies

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

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

9. Shell Programming and Scripting

Escape character

Hi , I want to change space to ' in my script. I tried doing this, sed 's/ /\'/g' filename but i could not get it. can some one help me please. Thanks, Deepak (4 Replies)
Discussion started by: deepakpv
4 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