views:

97

answers:

3

Basically, I am creating an XML file by taking the values from a column of a table. I am starting an AWK script from a shell script (ksh if it matters) like this:

SQL_RESULT=`sqlplus -s ${CONNECT_STRING} << EOF
${SQLPLUS_SETTINGS}
select customer_id from GD9_GENTH_CUST_SUBSCR;
exit;
EOF`
FILE_LIST=`echo $SQL_RESULT|sed -e 's/\n/''/g'`

echo $FILE_LIST|awk -f awk.file

The AWK script, awl.file, contains:

BEGIN {
          print "<?xml version=\"1.0\" encoding=\"UTF-8\"?><GenTransactionHandler  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"&gt;&lt;EntityToPublish&gt;\n&lt;Entity type=\"C\" typeDesc=\"Customer level\"><TargetApplCode>UHUNLD</TargetApplCode><TrxName>GET_CUST_DATA</TrxName>"
      }
      {
          print "<value>"$1"</value>"
      }
END
      {
          print "</Entity>\n</EntityToPublish></GenTransactionHandler>"
      }

When I run the script it gives me an AWK error.

What is the problem with this?

+2  A: 

If you are getting an error similar to this:

awk: syntax error at source line 9 source file xml.awk
 context is
    END >>> 
 <<< {
awk: bailing out at source line 12

Fix your code by moving the open bracket to the same line as END:

END {
    ...
Hai Vu
A: 

This line:

FILE_LIST=`echo "$SQL_RESULT"|sed -e 's/\n/''/g'`

could just as easily be:

FILE_LIST=`echo "$SQL_RESULT"|sed -e 's/\n//g'`

but neither one does anything. If your intention is to replace all the newlines with a null string then do this:

FILE_LIST=$( echo "$SQL_RESULT" | tr -d "\n" )

or with a space:

FILE_LIST=$( echo "$SQL_RESULT" | tr "\n" " " )

If you actually want to replace all the newlines with a pair of single quotes, that's a little more complicated:

FILE_LIST=$( echo "$SQL_RESULT" | sed -e '1{h};${x;s/\n/'\'\''/g;p};H;d' )
Dennis Williamson
you'll probably need to quote `"$SQL_RESULT"`
glenn jackman
You're right. I claim copy-paste-itis.
Dennis Williamson
A: 

since you are using awk already, it is not necessary to use sed or other tools like tr to replace newlines with nul.

@OP, problem may be in your quoting..but that's just a guess. show your sql output for further analysis

SQL_RESULT=`sqlplus -s ${CONNECT_STRING} << EOF
${SQLPLUS_SETTINGS}
select customer_id from GD9_GENTH_CUST_SUBSCR;
exit;
EOF` | awk 'BEGIN{
          q="\042"
          print "<?xml version="q"1.0"q" encoding="q"UTF-8"q"?><GenTransactionHandler xmlns:xsi="q"http://www.w3.org/2001/XMLSchema-instance"q"&gt;&lt;EntityToPublish&gt;\n&lt;Entity type="q"C"q" typeDesc="q"Customer level"q"><TargetApplCode>UHUNLD</TargetApplCode><TrxName>GET_CUST_DATA</TrxName>"
}
      {
          gsub("\n","")  # this is the same as your sed command...but also depends on SQL output
          print "<value>"$1"</value>"
      }
END
      {
          print "</Entity>\n</EntityToPublish></GenTransactionHandler>"
      }


}
'
ghostdog74