简体   繁体   English

使用awk命令选择文本

[英]selecting text using awk command

USERNAME=root
HOSTS="192.168.122.91 192.168.122.102 192.168.122.180"

SCRIPT="df -h /dev/vda3 | grep '/export/brick' | awk '{print $2}' ";
for HOSTNAME in ${HOSTS} ; do
   (ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}")
done

OUTPUT IS: 输出是:

/dev/vda3              27G  722M   26G   3% /export/brick

desired output = 27G 期望输出= 27G

You need to escape the $2 to have it interpreted by awk inside the ssh connection: 您需要转义$2以使其在ssh连接中由awk解释:

SCRIPT="df -h /dev/vda3 | grep '/export/brick' | awk '{print \$2}' ";

Also, note you can squeeze it a little bit, using the grep condition inside the awk : 另外,请注意你可以使用awkgrep条件稍微挤一下:

SCRIPT="df -h /dev/vda3 | awk '/export\/brick/{print \$2}' ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM