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 | |
by kroach (Pilgrim) on Feb 28, 2015 at 12:31 UTC | |
by karlgoethebier (Abbot) on Feb 28, 2015 at 15:50 UTC | |
by Bharath.M.R (Initiate) on Feb 28, 2015 at 16:07 UTC | |
by karlgoethebier (Abbot) on Feb 28, 2015 at 17:26 UTC | |
by Bharath.M.R (Initiate) on Feb 28, 2015 at 18:16 UTC | |
by karlgoethebier (Abbot) on Feb 28, 2015 at 18:44 UTC | |
by Bharath.M.R (Initiate) on Feb 28, 2015 at 19:43 UTC | |
| |
by Bharath.M.R (Initiate) on Feb 28, 2015 at 10:21 UTC |