hemlock hemlock - 2 years ago 93
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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download