2017-03-08 34 views
1

我从AWS这样的输出:SED正则表达式来提取领域,构建新的

$ aws ec2 describe-instances --filters "Name=instance-type,Values=c4.xlarge" | egrep -e "Value" -e "PrivateIpAddress" | grep bot -B1 
          "PrivateIpAddress": "10.100.2.44" 
          "Value": "bot-30", 
-- 
          "PrivateIpAddress": "10.100.2.106" 
          "Value": "bot-29", 
-- 
          "PrivateIpAddress": "10.100.2.167" 
          "Value": "bot-1", 
-- 
          "PrivateIpAddress": "10.100.2.161" 
          "Value": "bot-14", 
-- 
          "PrivateIpAddress": "10.100.2.235" 
          "Value": "bot-17", 
-- 
          "PrivateIpAddress": "10.100.2.54" 
          "Value": "bot-28", 
-- 
          "PrivateIpAddress": "10.100.2.234" 
          "Value": "bot-12", 
-- 
          "PrivateIpAddress": "10.100.2.43" 
          "Value": "bot-5", 
-- 
          "PrivateIpAddress": "10.100.2.50" 
          "Value": "bot-27", 
-- 
          "PrivateIpAddress": "10.100.2.124" 
          "Value": "bot-20", 
-- 
          "PrivateIpAddress": "10.100.2.247" 
          "Value": "bot-26", 
-- 
          "PrivateIpAddress": "10.100.2.113" 
          "Value": "bot-8", 
-- 
          "PrivateIpAddress": "10.100.2.190" 
          "Value": "bot-11", 
-- 
          "PrivateIpAddress": "10.100.2.184" 
          "Value": "bot-13", 
-- 
          "PrivateIpAddress": "10.100.2.253" 
          "Value": "bot-25", 
-- 
          "PrivateIpAddress": "10.100.2.254" 
          "Value": "bot-2", 
-- 
          "PrivateIpAddress": "10.100.2.199" 
          "Value": "bot-21", 
-- 
          "PrivateIpAddress": "10.100.2.130" 
          "Value": "bot-10", 
-- 
          "PrivateIpAddress": "10.100.2.59" 
          "Value": "bot-18", 
-- 
          "PrivateIpAddress": "10.100.2.4" 
          "Value": "bot-7", 
-- 
          "PrivateIpAddress": "10.100.2.213" 
          "Value": "bot-9", 
-- 
          "PrivateIpAddress": "10.100.2.214" 
          "Value": "bot-3", 
-- 
          "PrivateIpAddress": "10.100.2.200" 
          "Value": "bot-24", 
-- 
          "PrivateIpAddress": "10.100.2.148" 
          "Value": "bot-15", 
-- 
          "PrivateIpAddress": "10.100.2.146" 
          "Value": "bot-6", 
-- 
          "PrivateIpAddress": "10.100.2.94" 
          "Value": "bot-4", 
-- 
          "PrivateIpAddress": "10.100.2.150" 
          "Value": "bot-23", 
-- 
          "PrivateIpAddress": "10.100.2.81" 
          "Value": "bot-16", 
-- 
          "PrivateIpAddress": "10.100.2.155" 
          "Value": "bot-22", 
-- 
          "PrivateIpAddress": "10.100.2.102" 
          "Value": "bot-19", 

我想格式化成以下

10.100.2.44 bot-30 
10.100.2.106 bot-29 
10.100.2.167 bot-1 

的原因,我需要在格式,因为我要添加的所有那些主机名和ip地址在/etc/hosts文件中。这只是小列表,但我们也有很长的列表

回答

2

可以通过管道将您aws命令这sed

sed -n '/PrivateIpAddress/N;s/.*"\([^"]*\)"\n.*"\(.*\)",.*/\1\t\2/p' 
+0

你是第一个在所有所以'answered' – Satish

1

发现肮脏的方式来做到这一点。

$ aws ec2 describe-instances --filters "Name=instance-type,Values=c4.xlarge" | egrep -e "Value" -e "PrivateIpAddress" | grep bot -B1 | paste -d " " - - - | awk -F[\"] '{print $4 "\t" $8}' 
10.100.2.44 bot-30 
10.100.2.106 bot-29 
10.100.2.167 bot-1 
10.100.2.161 bot-14 
10.100.2.235 bot-17 
10.100.2.54 bot-28 
10.100.2.234 bot-12 
10.100.2.43 bot-5 
10.100.2.50 bot-27 
10.100.2.124 bot-20 
10.100.2.247 bot-26 
10.100.2.113 bot-8 
10.100.2.190 bot-11 
10.100.2.184 bot-13 
10.100.2.253 bot-25 
10.100.2.254 bot-2 
10.100.2.199 bot-21 
10.100.2.130 bot-10 
10.100.2.59 bot-18 
10.100.2.4 bot-7 
10.100.2.213 bot-9 
10.100.2.214 bot-3 
10.100.2.200 bot-24 
10.100.2.148 bot-15 
10.100.2.146 bot-6 
10.100.2.94 bot-4 
10.100.2.150 bot-23 
10.100.2.81 bot-16 
10.100.2.155 bot-22 
10.100.2.102 bot-19 
2

如果使用管的允许,我们可以试试下面的解决方案 -

awk '{ORS=(/,/?RS:FS)}1' f|awk -F'[,]|["]|[:][ ]' 'NR>1{printf "%-15s %s\n", $5,$10}' 
10.100.2.44  bot-30 
10.100.2.106 bot-29 
10.100.2.167 bot-1 
10.100.2.161 bot-14 
10.100.2.235 bot-17 
10.100.2.54  bot-28 
10.100.2.234 bot-12 
10.100.2.43  bot-5 
10.100.2.50  bot-27 
10.100.2.124 bot-20 
10.100.2.247 bot-26 
10.100.2.113 bot-8 
10.100.2.190 bot-11 
10.100.2.184 bot-13 
10.100.2.253 bot-25 
10.100.2.254 bot-2 
10.100.2.199 bot-21 
10.100.2.130 bot-10 
10.100.2.59  bot-18 
10.100.2.4  bot-7 
10.100.2.213 bot-9 
10.100.2.214 bot-3 
10.100.2.200 bot-24 
10.100.2.148 bot-15 
10.100.2.146 bot-6 
10.100.2.94  bot-4 
10.100.2.150 bot-23 
10.100.2.81  bot-16 
10.100.2.155 bot-22 
10.100.2.102 bot-19 
1
$ awk 'BEGIN{ RS="--"; FS=":"; OFS="\t" } # delimiters and such 
     { for(i=2;i<=3;i++)     # process $2 and $3 
      gsub(/ *"|".*/,"",$i)   # remove to first " and from second " 
      print $2,$3     # output $2 and $3 
     }' file        # or aws ... | awk ... 
10.100.2.44  bot-30 
10.100.2.106 bot-29 
1

单AWK应该足够了(最好是有一个测试结果的样本):

aws ec2 describe-instances --filters "Name=instance-type,Values=c4.xlarge" \ 
| awk -F '[":[:blank:]]+'!(/"PrivateIpAddress"/||/"Value".*bot/){next}{if($2=="PrivateIpAddress")IP=$3;else printf("%s\t%s\n",IP,$3)}' 

随着评论

# field separator is any combinaison of ": or space 
awk -F '[":[:blank:]]+' 
    # eliminate line (skip) that dont containt "private" or "value" with "bot" 
    !(/"PrivateIpAddress"/ || /"Value".*bot/){ next } 
     { 
     if line is Private, take IP value (only keep the last found) 
     if ($2 == "PrivateIpAddress") IP = $3 
     else { 
      # print line that have bot with last know IP 
      printf("%s\t%s\n", IP, $3) 
      } 
     }' 
1
awk '{print $4,$8}' RS=-- FS=\"