I want to execute awk command from tcl script

烈酒焚心 提交于 2020-01-05 10:27:10

问题


I want to execute the following command from my tcl script

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

I'm getting this error

awk: ^ invalid char ''' in expression

is it possible to execute such command from tcl

Thanks


回答1:


Quote from tcl man page:

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 += $1} END {print sum}' numbers.list

would be translated into something like:

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

So I would try without quotes (posted as answer as it can't fit properly in a comment, it's just a though from a quick search on google)

as per comment you may create the awk script in a var before like:

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



回答2:


Here is my solution:

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

If you don't need to show the output:

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

Note that in TCL, you don't group with single quote, but use either double quote or brace.



来源:https://stackoverflow.com/questions/28109655/i-want-to-execute-awk-command-from-tcl-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!