使用awk命令选择文本

时间:2014-02-05 10:10:41

标签: linux ssh awk

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

输出是:

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

所需输出= 27G

1 个答案:

答案 0 :(得分:2)

你需要转义$2以便在ssh连接中由awk解释它:

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

另外,请注意,您可以使用grep内的awk条件轻轻挤压它:

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