2

I'm trying search for a $ in a text document using a Perl one-liner in a Bash script. I suspect it is treating the $ as a 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?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
hemlock
  • 29
  • 3

2 Answers2

5

From the Bash manual:

Command Substitution

Command substitution allows the output of a command to replace the command name. There are two forms:

$(command)

or

`command` 

...

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \ ... When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

(emphasis added)

Bash is treating the \ in \$ as an escape sequence, so

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

actually runs

perl -ne 'print /$.*/g' file.txt

$. is a Perl built-in variable representing the current line number, so the regex becomes /1*/ for the first line, /2*/ for the second line, and so on. You'll get output for any line containing its own line number.

You can see this using Perl's re pragma:

$ ITEMVALUE=`perl -Mre=debug -ne 'print /\$.*/g' file.txt`
Compiling REx "1*"
...
Compiling REx "2*"
...

To fix, use the $(command) form of command substitution, which is generally preferred anyway:

ITEMVALUE=$(perl -ne 'print /\$.*/g' file.txt)
Community
  • 1
  • 1
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
0

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

Borodin
  • 126,100
  • 9
  • 70
  • 144
Sobrique
  • 52,974
  • 7
  • 60
  • 101