in reply to Re: Cutting last word from a output line
in thread Cutting last word from a output line

Hi Experts, First of all i want thank all and i am glad to be a part this forum. Thanks for the very quick response. My query is slightly different [not echoing a line , a command instead], sorry i posted slightly different. my requirement is like below. When i give a command like below ,
sysname =`snmpwalk.pl -c $string -t 100 + -r 2 $ipa +ddress sysName |awk '{print $NF}'
I get a output like below.
sysName.0 : OCTET STRING- (ascii): USWLS-P-TR03-C4510RE-1.adagnet.net
in that i want last word that is "USWLS-P-TR03-C4510RE-1.adagnet.net" to be assigned to a variable.

Replies are listed 'Best First'.
Re^3: Cutting last word from a output line
by kroach (Pilgrim) on Feb 28, 2015 at 12:31 UTC

    Replace \w (word character) with \S (non-whitespace) in the regular expression and it should work.

    my ($variable) = `your command` =~ /(\S+)$/;
Re^3: Cutting last word from a output line
by karlgoethebier (Abbot) on Feb 28, 2015 at 15:50 UTC

    I continue taking your specs literally.

    I assume that you:

    • can't modify snmpwalk.pl
    • want to set an environment variable

    So i jumped to the conclusion, that you need something like this:

    karls-mac-mini:monks karl$ sysname=$(./snmpwalk.pl | perl -ae 'print p +op @F') karls-mac-mini:monks karl$ echo $sysname USWLS-P-TR03-C4510RE-1.adagnet.net

    See also I know what I mean. Why don't you?.

    BTW, if you are on Windows, where does your awk come from? Cygwin? Or do you expect a port to PowerShell?

    Edit: Strucked out useless/annoying comment.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Thanks Karl, I just gave an example how i was achieving in unix shell. Ok i will try to re-phrase the question. I am expecting like below. Assume below is an system command: "snmpwalk.ovpl -c $string $IP sysName" here windows system command "snmpwalk.ovpl -c $string $IP sysName" will give me output like below. sysName.0 : OCTET STRING- (ascii): USWLS-P-TR03-C4510RE-1.adagnet.net So as said earlier i am expecting something like below.: my $variable = `system (sysName.0 : OCTET STRING- (ascii): USWLS-P-TR03-C4510RE-1.adagnet.net) | cut last word` expected $variable=USWLS-P-TR03-C4510RE-1.adagnet.net

        Thank you very much. So the solution given above by kroach works for you.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

      Thank you all, this is all i wanted to know :)
      ############################################### my $output = `nnmsnmpwalk.ovpl -u system -p manager123 -t 10 -c $Strin +g $IP sysName`; my $sysName = (split /\s/, $output)[-1]; print "system name is $sysName\n";
      THanks, Bharath

        Are you sure?

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re^3: Cutting last word from a output line
by Bharath.M.R (Initiate) on Feb 28, 2015 at 10:21 UTC
    I am running on winodws platform.