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).
In reply to Re: Cutting last word from a output line
by kroach
in thread Cutting last word from a output line
by Bharath.M.R
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |