Error in awk...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Error in awk...
# 1  
Old 04-02-2016
Error in awk...

Hi friends...
(Apoloigies for any typos.)
(Don, thanks for your input.)

Consider these two code snippets:-

awkerror1.awk
Code:
#!/usr/bin/awk -f
BEGIN \
{
	SAWTOOTHPLUS64 = " !#$&\'\)*,-/0235689:<>?ABDEGHJKMNPQSTVWYZ\\^_abdeghjkmnpqstvwyz|}~"
	for(LOOP = 1; LOOP <= 13; ++LOOP)
	{
		SAWTOOTHPLUS64 = SAWTOOTHPLUS64 SAWTOOTHPLUS64
	}
	printf("%s", SAWTOOTHPLUS64) > "/tmp/sawtoothplus64.raw"
}

awkerror2.awk
Code:
#!/bin/sh
awk 'BEGIN \
{
	SAWTOOTHPLUS64 = " !#$&"\'\)*,-/0235689:<>?ABDEGHJKMNPQSTVWYZ\\^_abdeghjkmnpqstvwyz|}~"
	for(LOOP = 1; LOOP <= 13; ++LOOP)
	{
		SAWTOOTHPLUS64 = SAWTOOTHPLUS64 SAWTOOTHPLUS64
	}
	printf("%s", SAWTOOTHPLUS64) > "/tmp/sawtoothplus64.raw"
}'

awkerror1.awk works perfectly.

awkerror2.awk gives this error:-
Code:
"""
./awkerror2.awk: line 9: unexpected EOF while looking for matching `"'
./awkerror2.awk: line 11: syntax error: unexpected end of file
"""

Last login: Sat Apr  2 07:52:50 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Awk
AMIGA:barrywalker~/Desktop/Code/Awk> ./awkerror1.awk
AMIGA:barrywalker~/Desktop/Code/Awk> ls -l /tmp/sawtoothplus64.raw
-rw-r--r--  1 barrywalker  wheel  524288  2 Apr 07:55 /tmp/sawtoothplus64.raw
AMIGA:barrywalker~/Desktop/Code/Awk> ./awkerror2.awk
./awkerror2.awk: line 9: unexpected EOF while looking for matching `"'
./awkerror2.awk: line 11: syntax error: unexpected end of file
AMIGA:barrywalker~/Desktop/Code/Awk> _

I have found out what is doing it, it is the ' inside the string, even escaping it does not cure the error, only removing it completely does.
I do not understand why a single quote, as part of a string inside double quotes, causes this error nor have I any idea how to cure it.
I have tried to get may amateur haed around it but unable to get it to work.
NOTE; this is more of curiosity as I am now using "#!/usr/bin/awk" for the script, but has "/bin/sh" or "awk" got a bug in parsing?

I await you guys knowledge...

TIA.

Last edited by wisecracker; 04-02-2016 at 04:27 AM..
# 2  
Old 04-02-2016
In the shell, double quote and backslash are regular characters inside a single quoted string. And, although single quote is not a special character inside a double quoted string, the single quote that is causing you problems is terminating a single-quoted string; not a single quote inside a double-quoted string in your awk script. The shell is seeing the single-quoted string:
Code:
'BEGIN \
{
	SAWTOOTHPLUS64 = " !#$&"\'

and both double quotes and single quotes are mismatched after that.

If you want a single quote for use inside a shell single-quoted awk script, define a variable outside the script and use the variable. For example:
Code:
#!/bin/sh
awk -v SQ="'" 'BEGIN \
{
	SAWTOOTHPLUS64 = " !#$&" SQ ")*,-/0235689:<>?ABDEGHJKMNPQSTVWYZ\\^_abdeghjkmnpqstvwyz|}~"
	for(LOOP = 1; LOOP <= 13; ++LOOP)
	{
		SAWTOOTHPLUS64 = SAWTOOTHPLUS64 SAWTOOTHPLUS64
	}
	printf("%s", SAWTOOTHPLUS64) > "/tmp/sawtoothplus64.raw"
}'

Note also that you don't need backslash escapes for the single quote and close parenthesis in the double-quoted string in awkerror1.awk (just as I dropped the backslash escape before the close parenthesis in the above adaptation of awkerror2.awk).
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-02-2016
Another technique is to split the ' ' into two parts and have a \' between them
Code:
echo 'part one'\''part two'

Actually, for a literal ' within ' ' it is enough to remember the escape sequence '\''
Code:
#!/bin/sh
awk 'BEGIN \
{
	SAWTOOTHPLUS64 = " !#$&'\'')*,-/0235689:<>?ABDEGHJKMNPQSTVWYZ\\^_abdeghjkmnpqstvwyz|}~"

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 04-02-2016
Hi Don and MadeInGermany...

Where would I be without you guys on here...

Thanks, consider this thread solved...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

No error in awk...

Hi all... In the OSX forum I am starting a new awk project to learn awk. In this code snippet I have had to check boundaries to ensure that no NUMERICAL error occurs in the rest of the code... printf "Enter frequency required:- "; getline FREQ; RATE=(BYTES*FREQ); if ( RATE <= 4000 ) {... (4 Replies)
Discussion started by: wisecracker
4 Replies

2. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

3. Shell Programming and Scripting

Error with awk

i have been trying to run the following bash script, but get error: awk: cmd. line:1: - awk: cmd. line:1: ^ unexpected newline or end of string I have been trying to figure out what the problem is but to no avail. I need to run the script urgently for a project, so any help will be highly... (5 Replies)
Discussion started by: anti_antaeus
5 Replies

4. Shell Programming and Scripting

awk Error

Hi I am trying to create a file with count of lines and string from shell variable, i am getting the error incorrect syntax. Below is the code : wc -l $filename | awk '{ print $1"|"$2"|"${SOURCETYPE} }'>>$AUDITFILENAME Could someone help me on this? (7 Replies)
Discussion started by: cnrj
7 Replies

5. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

6. Shell Programming and Scripting

AWK error

This is my disk space on solaris box and i wrote a simple script which identifies mount points which crosses 90% or above. But the AWK part is throwing some error and im unable to debug.. mzaheer @ UAT 144 % => df -k Filesystem kbytes used avail capacity Mounted on... (5 Replies)
Discussion started by: user__user3110
5 Replies

7. Shell Programming and Scripting

Awk error -- awk: 0602-562 Field $() is not correct.

typeset -i i=1 while read -r filename; do Splitfile=`$Targetfile_$i.txt` awk 'substr($0,1,5) == substr($filename,1,5) && substr($0,526,2) == substr($filename,6,2) && substr($0,750,12) == substr($filename,8,12)' $SourceFilename >> $Splitfile i=i+1 done < /tmp/list.out I am using this logic... (1 Reply)
Discussion started by: pukars4u
1 Replies

8. UNIX for Dummies Questions & Answers

awk error

Hi all, i have the files in the below sequence: fancy_LANG_STD_AU_2008-03-05.dat fancy_LANG_STD_HK_2008-03-06.dat fancy_LANG_STD_NZ_2008-03-05.dat fancy_STD_AU_2008-03-05.dat fancy_STD_HK_2008-03-06.dat fancy_STD_NZ_2008-03-05.dat i am trying to sort them like below: ... (3 Replies)
Discussion started by: gyankr
3 Replies

9. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

10. Shell Programming and Scripting

Error in awk

var1=`echo "emp,dept,salgrade" | awk -F, '{print NF}'` count=1 while ; do i=`expr $count` tname=`echo "emp,dept,salgrade" | awk -F, '{ print $(echo $i) }'` count=$count+1; echo ${tname}; echo $count done I want to store in tname=emp, tname=dept,tname=salgrade I am getting... (2 Replies)
Discussion started by: dreams5617
2 Replies
Login or Register to Ask a Question