2014-09-30 51 views
0
VOLUMES eu-west-1b 90  30    snap-54d6abac in-use  vol-a60879a4 gp2 
VOLUMES eu-west-1b 150  50    snap-8218247a in-use  vol-6ee29c6c gp2 
VOLUMES eu-west-1b 50      snap-8518247d in-use  vol-76e29c74 standard 
VOLUMES eu-west-1b 300  100    snap-bcef1047 in-use  vol-d22167d0 gp2 
VOLUMES eu-west-1b 30      snap-1e3ec2e6 in-use  vol-98efba9a standard 
VOLUMES eu-west-1b 24  8    snap-1b3ec2e3 in-use  vol-99efba9b gp2 
VOLUMES eu-west-1b False 24  8  snap-1b3ec2e3 available vol-4691b244 gp2 

我有以下脚本返回上面的结果 在/ usr/bin中/ AWS EC2描述体积--output文字| /斌/ grep的 “音量”查询使用AWK/egrep的提取特定字符串

我想只提取列(8) - 与VOL-

/usr/bin/aws ec2 describe-volumes --output text | /bin/grep "VOLUME" | /usr/bin/awk '{ print $6 }' 

启动IE的话我做了以下的变化,但这种返回

in-use 
in-use 
vol-76e29c74 
in-use 
vol-98efba9a 
in-use 
in-use 
vol-6591b267 
vol-01c6e403 
in-use 
in-use 
vol-0e416c0c 
vol-d1f28684 
vol-f7d5a2a2 
vol-84f4eca8 
available 

因为第5列对于某些行是空白的 - 所以结果看起来不正确。

有人能指出我如何提取只以“VOL-”

我不想用Perl语句 - 因为我如果安装了库我不知道,我需要使用的egrep或awk。

问候 d

回答

3

怎么样看向别的方向?

..... awk '{print $(NF-1)}' 
+1

为了完整性(并指出缺少对grep的需求):'/ usr/bin/aws ec2 describe-volumes --output text | awk'/ VOLUME/{print $(NF-1)}'' – 2014-09-30 17:04:54

0

grep的:

grep -oE '\bvol-\S+' 

用于上述示例打印

vol-a60879a4 
vol-6ee29c6c 
vol-76e29c74 
vol-d22167d0 
vol-98efba9a 
vol-99efba9b 
vol-4691b244 

二者里grep一起

aws ... | grep -oP '^VOLUMES.*\K\bvol-\S+' 

打印

vol-a60879a4 
.... 
etc..