在 for 循环中使用 awk 的 Bash 脚本
for i in $LIST
do
CFG=`ssh $i "cat log.txt|awk '{print $2}'"`
for j in $CFG
do
echo $j
done
done
假设我想打印一对远程主机上日志文件中的第二个字段。在上面的脚本中, print $2 不起作用。我该如何解决这个问题?谢谢。
for i in $LIST
do
CFG=`ssh $i "cat log.txt|awk '{print $2}'"`
for j in $CFG
do
echo $j
done
done
Say I want to print 2nd field in the log file on a couple remote host. In above script, print $2 doesn't work. How can I fix this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据 shell 扩展的数量和引用的类型,需要多个反斜杠转义:
作为一个技巧,您可以在美元符号和两者之间放置一个空格,以防止所有 $ 扩展。 awk 仍然会将它们粘合在一起:
Depending on the number of shell expansions and type of quoting multiple backslash escapes are needed:
As a trick you can put a space between the dollar sign and the two to prevent all $ expansion. Awk will still glue it together:
尝试
try
确保您从 shell 中转义
$2
- 您现在最终发送的 ssh 命令如下所示:ssh listvalue cat log.txt|awk '{print }'< /代码>
Make sure you're escaping the
$2
from the shell - the ssh command you end up sending right now is something like this:ssh listvalue cat log.txt|awk '{print }'
"
) 内扩展变量(以 $ 开头的单词
)。'
) 会阻止 Bash 查找 内部变量。single-quote
and thedouble-quote
.words beginning with $
) inside double-quote ("
).'
) stops Bash from looking for variables inside.user131527
andpsj
suggested, escaping$2
with\
should also have worked (depending on how your Bash is configured).我能够将 awk 放入双 for 循环
for i in 1 2 3 4;do
for j in $\(ls | awk -v I=$i '{print $I}');
回声 $j
完毕;
完毕
I was able to put awk in a double for loop
for i in 1 2 3 4;do
for j in $\(ls | awk -v I=$i '{print $I}');
echo $j
done;
done
失去猫。没用的..
Lose the cat. Its useless..