hemlock hemlock - 6 months ago 20
Perl Question

Perl One Liner Regex escaping $

I'm trying search for a

$
in a text document using a Perl one-liner in a bash script. I have a suspicion it is treating the
$
as variable.

This is my code:

ITEMVALUE=`perl -ne 'print /\$.*/g' file.txt`


The text looks something like this:

a bunch of random text
$80
a bunch of random text


I want to extract the
$80
only. Why isn't this working, and how do I retrieve the desired text?

Answer

It's simpler than that - that doesn't do what you think it does.

You probably want:

perl -ne 'print $1 if m/(\$\d+)/'

Which matches the regex, catches the value, and prints the value. (note - will match multiple times potentially).