当我在控制器机器上运行 cmd 时
Controller> echo DateTime Rd_avgMsec Wr_avgMsec; stats --diff --sho --int 60 --perf e2lxsplunkinx02-Hotdata --iosize --from 2016-10-11,00:00:00 --to 2016-10-11,00:03:00 | awk 'BEGIN{c=0} /^[0-9]|_avg/ {if (/^[0-9]/){printf "%s", $1;c++}else{ printf " %s", $NF/1000; c++ ; if(c==3){print ""; c=0}}}'
我得到以下输出:
DateTime Rd_avgMsec Wr_avgMsec
2016-10-11,00:01:00 2.732 0.21
2016-10-11,00:02:00 1.919 0.294
2016-10-11,00:03:00 1.856 0.22
但是当我尝试在控制器计算机上远程执行相同的命令时,我收到错误"awk: cmd. line:1: Unexpected token"
。我正在尝试从 launchpad01 linux 机器远程运行 cmd。任何人都可以帮我修复该错误吗?
[root@launchpad01 ~]# ssh admin@Controller "echo DateTime Rd_avgMsec Wr_avgMsec; stats --diff --sho --int 60 --perf e2lxsplunkinx02-Hotdata --iosize --from 2016-10-11,00:00:00 --to 2016-10-11,00:03:00 | awk 'BEGIN{c=0} /^[0-9]|_avg/ {if (/^[0-9]/){printf "%s", $1;c++}else{ printf " %s", $NF/1000; c++ ; if(c==3){print ""; c=0}}}'"
Warning: Permanently added 'Controller' (RSA) to the list of known hosts.
Password:
DateTime Rd_avgMsec Wr_avgMsec
awk: cmd. line:1: Unexpected token
答案1
这是一个引用问题。在这种情况下,最简单的解决方法是在本地运行 awk 管道:
ssh admin@Controller "stats --diff --sho --int 60 --perf e2lxsplunkinx02-Hotdata --iosize --from 2016-10-11,00:00:00 --to 2016-10-11,00:03:00" |\
awk 'BEGIN{c=0; print "DateTime","Rd_avgMsec","Wr_avgMsec";} /^[0-9]|_avg/ {if (/^[0-9]/){printf "%s", $1;c++}else{ printf " %s", $NF/1000; c++ ; if(c==3){print ""; c=0}}}'
答案2
如果您不想像建议的那样在本地运行 awk 管道鲁迪迈尔的答案,尝试转义命令中的双引号:
ssh admin@Controller "echo DateTime Rd_avgMsec Wr_avgMsec; \
stats --diff --sho --int 60 --perf e2lxsplunkinx02-Hotdata --iosize --from 2016-10-11,00:00:00 --to 2016-10-11,00:03:00 | \
awk 'BEGIN{c=0} /^[0-9]|_avg/ {if (/^[0-9]/){printf \"%s\", $1;c++}else{ printf \" %s\", $NF/1000; c++ ; if(c==3){print \"\"; c=0}}}'"