为什么这个 AWK 脚本会导致语法错误?

发布于 2024-08-08 03:46:22 字数 1002 浏览 3 评论 0原文

基本上,我通过从表的列中获取值来创建 XML 文件。 我正在从 shell 脚本(ksh 如果重要的话)如下:

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

AWK 脚本 awl.file 包含:

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

当我运行脚本时,它给我一个 AWK 错误。

这有什么问题吗?

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\"><EntityToPublish>\n<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北风几吹夏 2024-08-15 03:46:22

如果您收到与此类似的错误:

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

通过将左括号移至与 END 相同的行来修复代码:

END {
    ...

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 {
    ...
落日海湾 2024-08-15 03:46:22

这句话:

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

也可以很容易地是:

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

但没有人做任何事情。如果您的目的是用空字符串替换所有换行符,请执行以下操作:

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

或使用空格:

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

如果您实际上想用一对单引号替换所有换行符,那就有点复杂:

FILE_LIST=$( echo "$SQL_RESULT" | sed -e '1{h};${x;s/\n/'\'\''/g;p};H;d' )

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' )
涙—继续流 2024-08-15 03:46:22

由于您已经使用 awk,因此没有必要使用 sed 或其他工具(如 tr)将换行符替换为 nul。

@OP,问题可能出在你的引用中……但这只是一个猜测。显示您的 sql 输出以供进一步分析

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"><EntityToPublish>\n<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>"
      }


}
'

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"><EntityToPublish>\n<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>"
      }


}
'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文