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

Hi, I am going some Cisco show output parsing and print $_ unless is doing something weird (or at least weird to me)

$_ = PID: , VID: 255, SN: AGM163923J5

my line of perl is
print $_ unless /PID: ,/;

but the line still prints

if I use

print $_ unless /PID: /;

the unless works correctly and the line doesn't print (but this is no good to me as it matches lines I want). Why is the addition of "," stopping it from working. Interestingly Cisco IOS CLI is doing the same

"show inv |i PID: ," doesn't match the line

is it a regex thing ??

Replies are listed 'Best First'.
Re: Print unless behaving strangely
by johngg (Canon) on Jun 01, 2014 at 22:22 UTC

    There could be a non-printing character before the comma that is causing your match to fail; I inserted a NULL which doesn't show up when you print the line. You can inspect each character using split along with ord and sprintf inside a map, printing the ordinal value of each using say.

    $ perl -E ' $_ = qq{PID: \0, VID: 255, SN: AGM163923J5}; say; say for map {sprintf q{0x%02x}, ord } split m{};' PID: , VID: 255, SN: AGM163923J5 0x50 0x49 0x44 0x3a 0x20 0x00 <-- the unseen NULL 0x2c 0x20 0x56 0x49 0x44 0x3a 0x20 0x32 0x35 0x35 0x2c 0x20 0x53 0x4e 0x3a 0x20 0x41 0x47 0x4d 0x31 0x36 0x33 0x39 0x32 0x33 0x4a 0x35 $

    I hope this is helpful.

    Cheers,

    JohnGG

      this or any other "invisible" problem could also be visualized with Data::Dump

      use Data::Dump; $_ = qq{PID: \0, VID: 255, SN: AGM163923J5}; dd $_;

      "PID: \0, VID: 255, SN: AGM163923J5"

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Thanks for all the replies, I will try to answer them all

        Yes I did mean "$_ contains the value 'PID: , VID: 255, SN: AGM163923J5". So this wasn't a section of the perl code. I am reading a file and it is at this line that strangeness happens.

        Yes I am using Strict and Warnings

        I have put the output direct from the Cisco CLI through a Hex translator and there doesn't seem to be any hidden/null characters

        PID: , VID:

        0x500x490x440x3a0x200x2c0x200x560x490x440x3a

        but the output from the file perl is holding the string in does have extra characters.

        0x500x490x440x3a 0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x2c 0x560x490x440x3a

        Thanks for all your help

Re: Print unless behaving strangely
by Laurent_R (Canon) on Jun 01, 2014 at 22:08 UTC
    To me, the regex is correct. I tried to copy and paste the exact code that you posted under the Perl debugger:
    DB<1> $_ = "PID: , VID: 255, SN: AGM163923J5"; DB<2> print $_ unless /PID: ,/; DB<3>
    So it works as I expected (not printing $_ because of the successful match) and not as your reported result. Could it be that there are really two spaces between the colon after PID and the comma, or perhaps a tab and not a space, or some other special character? A starting point for the investigation might be to try this:
    print $_ unless /PID:\s+,/;
    to see if it matches or not.
Re: Print unless behaving strangely
by LanX (Saint) on Jun 01, 2014 at 21:29 UTC
    This $_ = PID: , VID: 255, SN: AGM163923J5 isn't Perl code°... did you forget quotes around the string?

    Do you use strict and warnings ?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    °) or at least not the one you expect. :-)

      I gave him the benefit of the doubt and assumed he was saying "$_ contains the value 'PID: , VID: 255, SN: AGM163923J5'. Here is what happens ...".

      Even so, this snippet works for me:

      my $foo = 'PID: , VID: 255, SN: AGM163923J5'; print "NOT MATCHED!\n" unless $foo =~ /PID: ,/;

      It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Print unless behaving strangely
by chrisayres11 (Novice) on Jun 03, 2014 at 10:32 UTC

    Thanks for all the replies, I will try to answer them all

    Yes I did mean "$_ contains the value 'PID: , VID: 255, SN: AGM163923J5". So this wasn't a section of the perl code. I am reading a file and it is at this line that strangeness happens.

    Yes I am using Strict and Warnings

    I have put the output direct from the Cisco CLI through a Hex translator and there doesn't seem to be any hidden/null characters

    PID: , VID:

    0x500x490x440x3a0x200x2c0x200x560x490x440x3a

    but the output from the file perl is holding the string in does have extra characters.

    0x500x490x440x3a 0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x7f0x2c 0x560x490x440x3a

    Thanks for all your help