问题
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.listwould 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