Quantcast
Channel: How to select first occurrence from between two patterns including them - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 3

How to select first occurrence from between two patterns including them

$
0
0

What I have:

interface GigabitEthernet0/2/1/6.2061
 description --> Some description here
 service-policy input BESTEFFORT-IN
 service-policy output Egress_30M
 vrf VPN-s2s
 ipv4 mtu 1500
 ipv4 address 222.222.222.1 255.255.255.252
 ipv4 unreachables disable
 dot1q vlan 2061
!
interface GigabitEthernet0/2/1/6.2062
 description Some Description here too
 service-policy input BESTEFFORT-IN
 vrf TRUSTED-3879
 ipv4 mtu 1500
 ipv4 address 111.111.111.111 255.255.255.252
--
[thousands of same data blocks]

What I want from that:

Get all data between pattern 1 (For example 2/1/6.2061) and the ends of the block (that shoudl be ! character)

Now i tried many ways with sed and awk as shown in many similar threads:

usr@mpls-fa-1:~/CIRCUITI_DA_DISMETTERE$ awk '/2\/1\/6.2061/{p=1} p; /\!/{exit}' idc3-sfc
usr@mpls-fa-1:~/CIRCUITI_DA_DISMETTERE$

And

usr@mpls-fa-1:~/CIRCUITI_DA_DISMETTERE$ sed -n -e '/2\/1\/6.2061/,/\!/p ; /\!/q' idc3-sfc
usr@mpls-fa-1:~/CIRCUITI_DA_DISMETTERE$

And much more variants like sed with !d and much more. If I use

sed -n -e '/2\/1\/6.2061/,/\!/p ;' idc3-sfc
awk '/2\/1\/6.2061/{p=1} p' idc3-sfc

it will shown any matches (so thousands and thousands of lines)

I post here just some threads i followed:


I found the problem by making a new file putting the example text into.

if i use:

usr@mpls-fa-1:~/CIRCUITI_DA_DISMETTERE$ awk '/2\/1\/6.2061/{p=1} p ; /!/{exit}' TEST
interface GigabitEthernet0/2/1/6.2061
 description --> Some description here
 service-policy input BESTEFFORT-IN
 service-policy output Egress_30M
 vrf VPN-s2s
 ipv4 mtu 1500
 ipv4 address 222.222.222.1 255.255.255.252
 ipv4 unreachables disable
 dot1q vlan 2061
!
usr@mpls-fa-1:~/CIRCUITI_DA_DISMETTERE$

it works [because it is the "first block of the text"] but if i try to get the second block by hitting:

usr@mpls-fa-1:~/CIRCUITI_DA_DISMETTERE$ awk '/2\/1\/6.2062/{p=1} p ; /!/{exit}' TEST
usr@mpls-fa-1:~/CIRCUITI_DA_DISMETTERE$

it doesn't work [because I'am searching interface 6.2062 instead of 6.2061]


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>