单次迭代后,使用awk从多行输出中选择性地打印(Selectively print from multiline output with awk after single iteration)

感谢您抽出宝贵时间阅读!

我试图通过awk指示Amazon EC2命令的输出,以获取传递给其他命令的信息。 问题是我需要从多个具有多个匹配点的多行获取输出。 这是一个例子:

我针对ec2发出以下命令:

ec2-describe-instances --filter tag:Name=webserver_name* --show-empty-fields

哪个输出:

RESERVATION r-RESERVATION_ID_1 OWNER_ID WebServers INSTANCE i-INSTANCE_ID_1 ami-IMAGE_ID_1 ec2-external-ip-of-instance_1.eu-west-1.compute.amazonaws.com ip-internal-ip-of-instance_1.eu-west-1.compute.internal running KEY_PAIR_NAME 0 (nil) m1.large date_and_time_start_1 REGION aki-KERNEL (nil) (nil) monitoring-disabled some.internal.ip.1 some.external.ip.1 (nil) (nil) ebs (nil) (nil) (nil) (nil) paravirtual xen (nil) sg-SECURITYGROUPID_1 default (nil) BLOCKDEVICE /dev/sda1 vol-VOLUME_ID_1 date_and_time_vol_start_1 true TAG instance i-INSTANCE_ID_1 Name webserver_name_1 RESERVATION r-RESERVATION_ID_2 OWNER_ID WebServers INSTANCE i-INSTANCE_ID_2 ami-IMAGE_ID_2 ec2-external-ip-of-instance_2.eu-west-1.compute.amazonaws.com ip-internal-ip-of-instance_2.eu-west-1.compute.internal running KEY_PAIR_NAME 0 (nil) m1.large date_and_time_start_2 REGION aki-KERNEL (nil) (nil) monitoring-disabled some.internal.ip.2 some.external.ip.2 (nil) (nil) ebs (nil) (nil) (nil) (nil) paravirtual xen (nil) sg-SECURITYGROUPID_2 default (nil) BLOCKDEVICE /dev/sda1 vol-VOLUME_ID_2 date_and_time_vol_start_2 true TAG instance i-INSTANCE_ID_2 Name webserver_name_2

其中每个实例的输出是四行,从RESERVATION,INSTANCE,BLOCKDEVICE和TAG开始。 您可以看到,上面的输出是针对两个实例,webserver_name_1和webserver_name_2。

我需要能够从该输出中获取以下内容:

第二行的第二个字段(i-INSTANCE_ID_1) 第三行的第三个字段(vol-VOLUME_ID_1) 第四行的第五个字段(webserver_name_1) 第六行的第二个字段(i-INSTANCE_ID_2) 第七行的第三个字段(vol-VOLUME_ID_2) 第八行的第五个字段(webserver_name_2)

这个命令是针对我们的ec2舰队发出的,所以需要继续上面的第十,第十一,第十二行,然后第十四,十五,十六等等

由于ec2-describe-instances命令需要很长时间才能返回输出,我不想再重复它来为每个实例返回输出; 我宁愿从单次迭代中获取输出并使用它。

我试图采取各种方法来检索此输出,但我无法单步执行每个实例结果。

感激不尽的任何帮助(并且非常感谢awk教程的任何好的指针)!

提前致谢。

thanks for taking the time to read!

I'm attempting to direct the output of an Amazon EC2 command through awk in order to obtain information to pass through to other commands. The issue is that I need to obtain output from multiple lines at multiple points that have multiple matches. Here's the example:

I issue the following command against ec2:

ec2-describe-instances --filter tag:Name=webserver_name* --show-empty-fields

which outputs:

RESERVATION r-RESERVATION_ID_1 OWNER_ID WebServers INSTANCE i-INSTANCE_ID_1 ami-IMAGE_ID_1 ec2-external-ip-of-instance_1.eu-west-1.compute.amazonaws.com ip-internal-ip-of-instance_1.eu-west-1.compute.internal running KEY_PAIR_NAME 0 (nil) m1.large date_and_time_start_1 REGION aki-KERNEL (nil) (nil) monitoring-disabled some.internal.ip.1 some.external.ip.1 (nil) (nil) ebs (nil) (nil) (nil) (nil) paravirtual xen (nil) sg-SECURITYGROUPID_1 default (nil) BLOCKDEVICE /dev/sda1 vol-VOLUME_ID_1 date_and_time_vol_start_1 true TAG instance i-INSTANCE_ID_1 Name webserver_name_1 RESERVATION r-RESERVATION_ID_2 OWNER_ID WebServers INSTANCE i-INSTANCE_ID_2 ami-IMAGE_ID_2 ec2-external-ip-of-instance_2.eu-west-1.compute.amazonaws.com ip-internal-ip-of-instance_2.eu-west-1.compute.internal running KEY_PAIR_NAME 0 (nil) m1.large date_and_time_start_2 REGION aki-KERNEL (nil) (nil) monitoring-disabled some.internal.ip.2 some.external.ip.2 (nil) (nil) ebs (nil) (nil) (nil) (nil) paravirtual xen (nil) sg-SECURITYGROUPID_2 default (nil) BLOCKDEVICE /dev/sda1 vol-VOLUME_ID_2 date_and_time_vol_start_2 true TAG instance i-INSTANCE_ID_2 Name webserver_name_2

where the output for each instance is four lines, starting with RESERVATION, INSTANCE, BLOCKDEVICE and TAG. As you may be able to see, the output above is for two instances, webserver_name_1 and webserver_name_2.

I need to be able to obtain the following from that output:

The second field from the second line (i-INSTANCE_ID_1) The third field from the third line (vol-VOLUME_ID_1) The fifth field from the fourth line (webserver_name_1) The second field from the sixth line (i-INSTANCE_ID_2) The third field from the seventh line (vol-VOLUME_ID_2) The fifth field from the eighth line (webserver_name_2)

and this command is to be issued against our ec2 fleet, so will need to continue as above with the tenth, eleventh, twelfth lines, then fourteenth, fifteenth, sixteenth etc etc

As the ec2-describe-instances command takes a long time to return output, I don't want to keep reiterating it to return output for each instance; I'd rather take the output from the single iteration and use that.

I've attempted to take various ways of retrieving this output but I am having difficulty stepping through each instances result.

Any help gratefully received (and any good pointers to awk tutorials also gratefully received) !!

Thanks in advance.

最满意答案

我不确定我理解你的问题,但认为你可能正在寻找这样的事情:

someEC2command | awk ' /^INSTANCE/ {instance_id=$2} /^BLOCKDEVICE/ {volid=$3} /^TAG/ {tag=$5;print instance_id,volid,tag}'

它给出了输出:

i-INSTANCE_ID_1 vol-VOLUME_ID_1 webserver_name_1 i-INSTANCE_ID_2 vol-VOLUME_ID_2 webserver_name_2

因此,当它看到行开始“INSTANCE”时,它会选择字段2,当它看到以“BLOCKDEVICE”开头的行时,它从field3中获取volid,并且每次看到行开始“TAG”时它会拾取标签然后打印任何内容它到目前为止已经找到了。

关于awk教程/参考...你只需要一个,请看这里 。

I am not sure I understand your question, but think you may be looking for something like this:

someEC2command | awk ' /^INSTANCE/ {instance_id=$2} /^BLOCKDEVICE/ {volid=$3} /^TAG/ {tag=$5;print instance_id,volid,tag}'

which gives output:

i-INSTANCE_ID_1 vol-VOLUME_ID_1 webserver_name_1 i-INSTANCE_ID_2 vol-VOLUME_ID_2 webserver_name_2

So, when it sees lines starting "INSTANCE" it picks up field 2, when it sees lines starting "BLOCKDEVICE" it picks up volid from field3, and every time it sees lines starting "TAG" it picks up the tag and then prints whatever it has found so far.

As regards an awk tutorial/reference... youy only need one, see here.

更多推荐