我想从 tcl 脚本执行 awk 命令

I want to execute awk command from tcl script

我想从我的 tcl 脚本中执行以下命令

exec /bin/awk '/Start/{f=1;++file}/END/{f=0}f{print > "/home/user/report/"file }' input

我遇到了这个错误

awk: ^ invalid char ''' in expression

是否可以从 tcl 执行这样的命令

谢谢

引自 tcl 手册页:

When translating a command from a Unix shell invocation, care should be taken over the fact that single quote characters have no special significance to Tcl. Thus:

awk '{sum += } END {print sum}' numbers.list

would be translated into something like:

exec awk {{sum += } END {print sum}} numbers.list

所以我会尝试不加引号(作为答案发布,因为它不能正确地放在评论中,这只是在 google 上快速搜索的结果)

根据评论,您可以在 var 之前创建 awk 脚本,例如:

set awk_command "/Start/{f=1;++file}/END/{f=0}f{print > \"$tcl_variable\"file }" 
exec /bin/awk $awk_command input

这是我的解决方案:

puts [exec /usr/bin/awk \
           {/Start/{f=1;++file}END{f=0}f{print > file}} \
           invoke_awk.txt]

如果不需要显示输出:

exec /usr/bin/awk \
     {/Start/{f=1;++file}END{f=0}f{print > file}} \
     invoke_awk.txt

请注意,在 TCL 中,您不使用单引号进行分组,而是使用双引号或大括号。