2014-09-30 20 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..