Operating System - Linux
1753378 Members
5002 Online
108792 Solutions
New Discussion

Re: system() with sprintf()

 
Chris Frangandonis
Regular Advisor

system() with sprintf()

Hi All,

I am experiencing a small problem with single quotes in a awk script

If I use the sprintf cmd shown below it returns the following error mesg

selcmd1=sprintf("%s","echo select A_NUM=nullif\(A_NUM,\\\" \\\"\) R_DT=date\('2006/06/06'\) "\\\\ngo | " opt)


select A_NUM=nullif\(A_NUM,\" \"\) from CE.CDR_DRMS where R_DT=date\(2006/06/06\) and A_1=0 and A_2=1 and A_3=2 and A_4=3 and A_5=1 and A_6=1 and A_7=8 and A_8=8 and A_9=6 and A_10=isnull\(5,\"\"\) \\ngo | /sybase_oc/OCS-12_0/bin/isql -UDBA -PSQL -Sdrms_iq CHATAA
Msg 257, Level 16, State 0:
ASA Error -157: Cannot convert 55 to a date

I know it is doing an mathematical sum.


Code:

function select_tablename(selcmd1,dc)
{
selcmd1="echo select A_NUM=nullif\\(A_NUM,\\\" \\\"\\) from CE.CDR_DRMS where R_DT=date\\('2006/06/06'\\) and A_1="Dig1" and A_2="Dig2" and A_3="Dig3" and A_4="Dig4" and A_5="Dig5" and A_6="Dig6" and A_7="Dig7" and A_8="Dig8" and A_9="Dig9" and A_10=isnull\("Dig10",\\\"\\\"\) \\\\ngo | " opt
if (dc = system(selcmd1) != 0)
{
PLogmsg(2, "Failed ")
}
close(selcmd1)
system(selcmd1)
}

Instead of me doing a date conversion , how can I use with single quotes in the R_DT=('2006/06/06 00:00:00')


Many Thanks
Chris
1 REPLY 1
Peter Nikitka
Honored Contributor

Re: system() with sprintf()

Hi,

since it seems tat you need both single- and doublequotes in your awk-script, we must know, if your script is (I call it so) standalone or embedded in another shellscript.
# Stanbdalone
#!/usr/bin/awk
function select_tablename ....

Enbedded:
#!/usr/bin/ksh
...
awk 'function select_tablename ...'
...

In the later case, I usually call

awk -v sq="'" -v dq='"' '...'

and use the variables sq for single quotes and dq for double quotes. I find the code is more readable, when you know that a double quote belongs solely to your program code.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"