hemlock hemlock - 7 months ago 33
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
a bunch of random text

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


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