Bharath.M.R has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts, I am new to perl. I want to cut the last word from command line output from one of the command AND assign that to a variable so that i can use it in my rest of the program. Like as in shell variable =`echo $line| awk -F, '{print $NF}'` please suggest.

Update:

. 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

Replies are listed 'Best First'.
Re: Cutting last word from a output line
by kroach (Pilgrim) on Feb 27, 2015 at 22:05 UTC

    To get only the last word of a commands output:

    my ($variable) = `echo $line` =~ /(\w+)$/;

    The parentheses around $variable enforce list context, which assigns the only pattern match of /(\w+)$/ (which is the last word of the command output) to $variable instead of just evaluating whether the string matches that pattern.

    Had the parentheses been omitted, $variable would contain 1 if there were any words at the end of your commmands output and 0 otherwise.


    If you want to actually remove the last word from a string, you can use regex substitution and capture the match:

    my $output = `echo $line`; $output =~ s/(\w+)$//; my $last_word = $1;

    The last word gets captured into the $1 variable replaced with an empty string (i.e. removed).

      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.

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

        my ($variable) = `your command` =~ /(\S+)$/;

        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»

        I am running on winodws platform.
Re: Cutting last word from a output line
by GotToBTru (Prior) on Feb 27, 2015 at 19:37 UTC

    What do you have so far? The shift command will return each command line parameter in order. split /\s+/,$string will divide $string up by spaces, which might work for dividing the command output into words.

    Dum Spiro Spero
Re: Cutting last word from a output line
by karlgoethebier (Abbot) on Feb 27, 2015 at 19:43 UTC

    I assume you mean something like this:

    #!/usr/bin/env perl -a use strict; use warnings; use feature qw (say); my $var = pop @F; say $var; __END__ karls-mac-mini:monks karl$ echo foo bar | ./1118114.pl bar

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Cutting last word from a output line
by crusty_collins (Friar) on Feb 27, 2015 at 19:39 UTC
    why not do this $str =~ /(\w+)$/;
Re: Cutting last word from a output line
by fishmonger (Chaplain) on Feb 28, 2015 at 19:47 UTC