如何grep特定字符串并仅显示特定列

时间:2013-12-12 18:12:03

标签: perl grep

我有一个包含元素的数组:

set interfaces ge-1/0/2 unit 108 family inet address 10.187.132.227/27 vrrp-group 8 virtual-address 10.187.132.225
set interfaces ge-1/0/2 unit 108 family inet address 10.187.132.227/27 vrrp-group 8 priority 200
set interfaces ge-1/0/2 unit 108 family inet address 10.187.132.227/27 vrrp-group 8 accept-data
set interfaces ge-1/0/2 unit 109 family inet address 10.187.133.3/27 vrrp-group 9 virtual-address 10.187.133.1
set interfaces ge-1/0/2 unit 109 family inet address 10.187.133.3/27 vrrp-group 9 priority 190
set interfaces ge-1/0/2 unit 109 family inet address 10.187.133.3/27 vrrp-group 9 accept-data
set interfaces ge-1/0/2 unit 110 family inet address 10.187.133.35/27 vrrp-group 10 virtual-address 10.187.133.33
set interfaces ge-1/0/2 unit 110 family inet address 10.187.133.35/27 vrrp-group 10 priority 200
set interfaces ge-1/0/2 unit 110 family inet address 10.187.133.35/27 vrrp-group 10 accept-data
set interfaces ge-1/0/2 unit 111 family inet address 10.187.133.67/27 vrrp-group 11 virtual-address 10.187.133.65
set interfaces ge-1/0/2 unit 111 family inet address 10.187.133.67/27 vrrp-group 11 priority 190
set interfaces ge-1/0/2 unit 111 family inet address 10.187.133.67/27 vrrp-group 11 accept-data
set interfaces ge-1/0/2 unit 112 family inet address 10.187.133.99/27 vrrp-group 12 virtual-address 10.187.133.97

如何在地址和vrrp-group之间仅使用grep?

2 个答案:

答案 0 :(得分:1)

 my @array = ...

 my @strings = map {/address\s*(.*?)\s*vrrp-group/ ? $1 : ()} @array;

答案 1 :(得分:-1)

假设每一行都是一个元素,split空格上的每个元素将它变成一个数组,在相关的数组元素中看起来。