sed multiline pattern match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed multiline pattern match
# 1  
Old 05-10-2012
sed multiline pattern match

How can I write a script that takes a cisco config file and outputs every occurrence of two, or more, pattern matches through the whole config file?

For example, out of a config file, i want to print out every line with interface, description and ip address through the whole file, and disregard anything else.

Example:

Code:
interface Loopback1111
 description aaaa
 ip vrf forwarding vaaaa
 ip address 1.1.1.1
!
interface Loopback2222
 description bbbb
 ip vrf forwarding vbbbb
 ip address 2.2.2.2
!

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 05-11-2012 at 06:39 AM..
# 2  
Old 05-10-2012
Code:
cat test.txt

interface Loopback1111
description aaaa
ip vrf forwarding vaaaa
ip address 1.1.1.1
!
interface Loopback2222
description bbbb
ip vrf forwarding vbbbb
ip address 2.2.2.2
!


sed -n '/interface\|description\|ip/p'  test.txt

interface Loopback1111
description aaaa
ip vrf forwarding vaaaa
ip address 1.1.1.1
interface Loopback2222
description bbbb
ip vrf forwarding vbbbb
ip address 2.2.2.2

# 3  
Old 05-10-2012
are there any good books to read up on this? can you recommend any?

---------- Post updated at 08:11 PM ---------- Previous update was at 07:54 PM ----------

I think i have a more clear question after seeing this. Given the lab config below, say that i want to print out a list of interfaces that belong to vrf ce1 (via command vrf forwarding). I want the list to include the interface, vrf forwarding name (i.e. ce1) and IP address; and ignore any other config options that contain any of the same information. In other words, I am looking only to list certain lines of text in the interface configuration portion and nothing else.

An example result would be, from the config file below:

Original Config text:
Code:
(a bunch of stuff above)
interface Serial1/0
 ip vrf forwarding ce1
 ip address 166.1.3.2 255.255.255.252
 encapsulation ppp
 fair-queue
 serial restart-delay 0
(a bunch of stuff below)

My results:

Code:
interface Serial1/0
 ip vrf forwarding ce1
 ip address 166.1.3.2 255.255.255.252





Code:
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
!
hostname pe1
!
boot-start-marker
boot-end-marker
!
enable password 7 1307121719
!
no aaa new-model
!
resource policy
!
ip subnet-zero
ip cef
!
!
no ip dhcp use vrf connected
!
!
ip vrf ce1
 rd 100:1
 route-target export 100:1
 route-target import 100:2
 route-target import 1111:1111
 route-target import 2222:2222
 route-target import 100:100
!
ip vrf forwarding
!
no ip domain lookup
no ip ips deny-action ips-interface
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
class-map match-all ce-pe-in
 match  dscp ef 
!
!
policy-map ce-pe-in
 class ce-pe-in
  priority 16000 8000
!
! 
!
!
!
!
interface Loopback100
 ip address 193.254.254.1 255.255.255.255
!
interface Ethernet0/0
 no ip address
 shutdown
 duplex auto
!
interface GigabitEthernet0/0
 ip address 10.10.10.13 255.255.255.0
 duplex full
 speed 1000
 media-type gbic
 negotiation auto
!
interface Serial1/0
 ip vrf forwarding ce1
 ip address 166.1.3.2 255.255.255.252
 encapsulation ppp
 fair-queue
 serial restart-delay 0
!
interface Serial1/1
 no ip address
 shutdown
 serial restart-delay 0
!
interface Serial1/2
 no ip address
 shutdown
 serial restart-delay 0
!
interface Serial1/3
 no ip address
 shutdown
 serial restart-delay 0
!
interface FastEthernet2/0
 ip address 192.168.1.1 255.255.255.252
 ip ospf cost 1
 duplex auto
 speed auto
 mpls label protocol ldp
 mpls ip
!
interface FastEthernet2/1
 no ip address
 shutdown
 duplex auto
 speed auto
!
router ospf 100
 log-adjacency-changes
 network 192.168.0.0 0.0.255.255 area 0
 network 193.168.1.1 0.0.0.0 area 0
!
router bgp 100
 no synchronization
 bgp always-compare-med
 bgp log-neighbor-changes
 neighbor 193.254.254.24 remote-as 100
 neighbor 193.254.254.24 update-source Loopback100
 neighbor 193.254.254.24 send-community both
 no auto-summary
 !
 address-family vpnv4
 neighbor 193.254.254.24 activate
 neighbor 193.254.254.24 send-community both
 exit-address-family
 !
 address-family ipv4 vrf ce1
 redistribute connected
 neighbor 166.1.3.1 remote-as 65001
 neighbor 166.1.3.1 activate
 maximum-paths eibgp 2
 no auto-summary
 no synchronization
 bgp suppress-inactive
 exit-address-family
!
ip classless
no ip http server
no ip http secure-server
!
ip as-path access-list 1 permit ^$
!
!
!
ip prefix-list no-internal-def seq 5 permit 0.0.0.0/0
!
ip prefix-list no-internal-ip seq 5 permit 0.0.0.0/0 le 29
snmp-server community beer RW
!
route-map internal deny 5
 match ip address prefix-list no-internal-def no-internal-ip
 match as-path 1
!
route-map internal permit 6
!
!
!
!
control-plane
!
!
!
!
!
!
gatekeeper
 shutdown
!
!
line con 0
 exec-timeout 0 0
 stopbits 1
line aux 0
 stopbits 1
line vty 0 4
 password 7 094E4B0C0B
 login
!
!
end


Last edited by Scrutinizer; 05-11-2012 at 07:25 AM..
# 4  
Old 05-11-2012
I would go for sed & awk of Dale Dougherty & Arnold Robbins. Online you can find lot of material.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

2. Shell Programming and Scripting

Pattern match with awk/sed - help

I need to grep for the pattern text inside the square brackets which are in red and not in green..my current code greps patterns both of them, which i don't want Input fileref|XP_002371341.1| oxoacyl-ACP reductase, putative gb|EPT24759.1| 3-ketoacyl-(acyl-carrier-protein) reductase ... (2 Replies)
Discussion started by: selvankj
2 Replies

3. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

4. Shell Programming and Scripting

Using sed to pattern match within a particular multiline block and take action

Hi all, This is my first post, so please go easy if I broke some rules. Not accustomed to posting in forums... :) I'm looking for help on pattern matching within a multiline block and looking to highlight blocks/block-ids that do NOT contain a particular pattern. For example an input file... (5 Replies)
Discussion started by: tirodad
5 Replies

5. Shell Programming and Scripting

Sed Pattern Match

Hi, I would like to use SED to do the following string replacement: asd1abc to www1cda asd2abc to www2cda ... asd9abc to www9cda I can use 'asd.abc' to find the orignal string, however I don't know how to generate the target string. Any suggestion? Thanks, ... (2 Replies)
Discussion started by: mail4mz
2 Replies

6. Shell Programming and Scripting

Match a pattern starting with sub-pattern using sed

Hi all, I've been experiencing a difficulty trying to match a number and write it to a new file. My input file is: input.txt It contains the lines: 103P 123587.256971 3.21472112 3.1517423 1.05897234566427 58.2146258 12.35478 25.3612489 What would be the sed command to... (17 Replies)
Discussion started by: Biederman
17 Replies

7. Shell Programming and Scripting

sed pattern match problem

Hi all, hoping this is a simple one, tried looking but just can't see the solution As an example I've got a list of words that all start Ben..... Bendicks Benefiber Ben Benylin I need to only change the line Ben with Ben 10, ignoring the other lines. I tried the following ... (1 Reply)
Discussion started by: mrpugster
1 Replies

8. Shell Programming and Scripting

Multiline pattern search using sed or awk

Hi friends, Could you please help me to resolve the below issue. Input file :- <Node> <username>abc</username> <password>ABC</password> <Node> <Node> <username>xyz</username> <password>XYZ</password> <Node> <Node> <username>mnp</username> ... (3 Replies)
Discussion started by: haiksuresh
3 Replies

9. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

10. Shell Programming and Scripting

Awk match a multiline pattern

Hello! i wanna match in a config file, one text with more than one lines, something like this: CACHE_SIZE{ 10000 M } I have problems with the ends of line, i think that i can match the end of the line with \n, but i can't get it Someone can help me with the regular expression? ... (18 Replies)
Discussion started by: claw82
18 Replies
Login or Register to Ask a Question