简体   繁体   English

AWK打印$ 2 Breaking Bash Args

[英]AWK Print $2 Breaking Bash Args

I'm trying to capture the output from an EC2 command with AWK. 我正在尝试使用AWK捕获EC2命令的输出。 The AWK portion works and the EC2 command work. AWK部分起作用,而EC2命令起作用。 The problem is, I pass arguments to the script and one of them conflicts with the style of AWK. 问题是,我将参数传递给脚本,其中之一与AWK的样式冲突。 Specifically print $2 具体print $2

Proper segment is 正确的细分是

cmd="/opt/aws/bin/ec2-run-instances -O $secid  -W $seckey $ami  -n $1 -g $secg -k $sshkey -t $instsize -z $2 | awk '/^INSTANCE/ {print $2}'

As you can see, I need that print $2 to capture the EC2 instance id. 如您所见,我需要打印$ 2来捕获EC2实例ID。 Is there a workaround without changing my arg format? 有没有不更改我的arg格式的解决方法?

Thanks! 谢谢!

RESERVATION     r-******      ********    www.abc.com
INSTANCE        i-****      ami-*****

I believe you just need to escape it for awk so that shell doesn't replace $1 , $2 etc, liek this: 我相信您只需要为awk对其进行转义,以使shell不会替换$1$2等,请注意以下几点:

awk '/^INSTANCE/ {print \$2}'

OR your CMD variable: 或您的CMD变量:

cmd="/opt/aws/bin/ec2-run-instances -O $secid  -W $seckey $ami  -n $1 -g $secg -k $sshkey -t $instsize -z $2 | awk '/^INSTANCE/ {print \$2}'"
result=$(/opt/aws/bin/ec2-run-instances -O $secid  -W $seckey $ami  -n $1 -g $secg -k $sshkey -t $instsize -z $2 | awk '/^INSTANCE/ {print $2}')
for instance_id in $result; do echo $instance_id; done

尝试这样的事情:

cmd="/opt/aws/bin/ec2-run-instances -O $secid  -W $seckey $ami  -n $1 -g $secg -k $sshkey -t $instsize -z $2 | awk -v var=$2 '/^INSTANCE/ {print var}'

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

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