Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

search for a string and return previous relating string if found

by Anonymous Monk
on Dec 06, 2017 at 17:09 UTC ( [id://1205031]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have the following text in many files, standard Cisco switch stuff and I need to script something that will parse many text files and print if the command "spanning-tree guard root" is enabled on each interface. i.e. I want to search where is enabled, and return the file\interface in a print.

! interface FastEthernet1/0/3 switchport access vlan 300 switchport mode access switchport port-security maximum 130 switchport port-security switchport port-security aging time 2 switchport port-security aging type inactivity no logging event link-status speed 100 duplex full srr-queue bandwidth share 1 60 30 10 srr-queue bandwidth shape 3 0 0 0 mls qos trust device cisco-phone storm-control broadcast level pps 1k storm-control multicast level pps 2k storm-control action shutdown storm-control action trap spanning-tree portfast spanning-tree bpduguard enable spanning-tree guard root !

Replies are listed 'Best First'.
Re: search for a string and return previous relating string if found
by tybalt89 (Monsignor) on Dec 06, 2017 at 19:56 UTC
    #!/usr/bin/perl use strict; use warnings; $/ = "!\n"; while( <DATA> ) { /^interface (.*\n)(?: .*\n)*? spanning-tree guard root/ and print $1 +; } __DATA__ ! interface other-one switchport access vlan 300 switchport mode access switchport port-security maximum 130 switchport port-security switchport port-security aging time 2 switchport port-security aging type inactivity no logging event link-status speed 100 duplex full srr-queue bandwidth share 1 60 30 10 srr-queue bandwidth shape 3 0 0 0 mls qos trust device cisco-phone storm-control broadcast level pps 1k storm-control multicast level pps 2k storm-control action shutdown storm-control action trap spanning-tree portfast spanning-tree bpduguard enable ! ! interface FastEthernet1/0/3 switchport access vlan 300 switchport mode access switchport port-security maximum 130 switchport port-security switchport port-security aging time 2 switchport port-security aging type inactivity no logging event link-status speed 100 duplex full srr-queue bandwidth share 1 60 30 10 srr-queue bandwidth shape 3 0 0 0 mls qos trust device cisco-phone storm-control broadcast level pps 1k storm-control multicast level pps 2k storm-control action shutdown storm-control action trap spanning-tree portfast spanning-tree bpduguard enable spanning-tree guard root ! ! interface other-two switchport access vlan 300 switchport mode access switchport port-security maximum 130 switchport port-security switchport port-security aging time 2 switchport port-security aging type inactivity no logging event link-status speed 100 duplex full srr-queue bandwidth share 1 60 30 10 srr-queue bandwidth shape 3 0 0 0 mls qos trust device cisco-phone storm-control broadcast level pps 1k storm-control multicast level pps 2k storm-control action shutdown storm-control action trap spanning-tree portfast spanning-tree bpduguard enable ! ! interface FastEthernet1/0/4 switchport access vlan 300 switchport mode access switchport port-security maximum 130 switchport port-security switchport port-security aging time 2 switchport port-security aging type inactivity no logging event link-status speed 100 duplex full srr-queue bandwidth share 1 60 30 10 srr-queue bandwidth shape 3 0 0 0 mls qos trust device cisco-phone storm-control broadcast level pps 1k storm-control multicast level pps 2k storm-control action shutdown storm-control action trap spanning-tree portfast spanning-tree bpduguard enable spanning-tree guard root !

    Outputs:

    FastEthernet1/0/3 FastEthernet1/0/4
Re: search for a string and return previous relating string if found
by NetWallah (Canon) on Dec 06, 2017 at 19:54 UTC
    Try this:
    perl -nE "m/^interface (\S+)/ and $int=$1; m/ spanning-tree guard root +/ and print qq|$int $_|" YOUR-FILE.txt
    prints:
    FastEthernet1/0/3 spanning-tree guard root
    Somewhat simple-minded, but should do the trick.

                    All power corrupts, but we need electricity.

Re: search for a string and return previous relating string if found
by hippo (Bishop) on Dec 06, 2017 at 17:14 UTC

    Sounds fairly straightforward. What did you try and in what way did it fail?

      you caught me out ;-] if have been using powershell select-string, but am having issues with it searching between the !! delimeters in the text files. using a context produces the lines above or below the match, but this is always variable as the code is different on each section of the input file.

       Select-String -Pattern "" -context 2  *.txt

      I use a basic perl script to search through for single matches but this stuff is beyond my grasp of Perl

Re: search for a string and return previous relating string if found
by poj (Abbot) on Dec 06, 2017 at 20:10 UTC
    I have the following text in many files

    Are all the files in one directory and do the filenames have a pattern ?

    poj

      the files are all in one folder, the only unique identifier of the files is they are all .txt

        In that case then glob would find the files

        #!/usr/bin/perl use strict; my $dir = 'c:/temp/tmp'; # directory to search my @files = glob("$dir/*.txt"); for my $filename (@files){ my $lineno = 0; print "Scanning $filename ..\n"; open IN,'<',$filename or die "Could not open $filename : $!"; my $iface; while (<IN>){ ++$lineno; if (/^interface\s+(.*)/){ $iface = $1; } if (/spanning-tree guard/){ print "$filename,$lineno,$iface,$_"; $iface = ''; } } }
        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1205031]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-03-28 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found