in reply to Cutting last word from a output line

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).

Replies are listed 'Best First'.
Re^2: Cutting last word from a output line
by Bharath.M.R (Initiate) on Feb 28, 2015 at 09:44 UTC
    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»

        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 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
      I am running on winodws platform.