The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 03-11-2008
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
You're welcome. There are many ways to do this.

Here's the explanation:

echo "1.39," | perl -pe 's/.$//'

perl -pe
This calls perl. The "p" causes Perl to pass through the input to the output, whether or not it is modified along the way. The "e" indicates to Perl that the expression (code) comes next.

The expression is a simple regex substitution. The period stands for any character, and the dollar sign means "end of line." So this regular expression matches any character at the end of the line. The second part of the regular expression was left empty, so if the first part matches, it's replaced with nothing.

Here's a more verbose regular expression, in Perl syntax, just FYI.

$line =~ s/fred$/barney/;

Here, I substitute "fred" at the end of the line with "barney." In the shorter example, I wanted to eliminate something, so there was nothing between the final two forward-slashes. Also, I didn't use the "variable =~" syntax, because in a Perl one-liner the line of input is assumed by Perl. It can also be explicitly referred to with the $_.

So these two are identical:

echo "1.39," | perl -pe 's/.$//'

echo "1.39," | perl -pe '$_ =~ s/.$//'

Finally, the "=~" syntax sets $_ to the result of running the regular expression substitution on it. In the shorter version that is implicit, and Perl understands it.

ShawnMilo