escaping "$" in command line arguments

T

Tiro Verus

Can this be done?

I've been trying to count the number of times a given word
appears in an ASCII file. This works for most terms:
==============================
my $search_term = shift;

my $count = 0;
while (<>) {
while (m/\b$search_this\b/g) {
$count++;
}
}
================================

But it doesn't take input search terms beginning with $
such as $fn. I've tried

$./wordcount.pl \$fn textfile

and

$./wordcount.pl '\$fn' textfile

as well as

$./wordcount.pl $fn textfile


Which gives the message "undefined variable" or something like that.
Or the output says " textfile does not contain $fn" when it does.
That was for the '$fn' version. But 'fn' in @ARGV works just fine.
[The code lines with the result msgs have been snipped]

I cannot find anything about this in the Camel Book or the
Cookbook. BTW

$ grep '\$fn' textfile

finds all the lines with "$fn" in them, but I need an occurrence count.
 
R

RedGrittyBrick

Tiro said:
Can this be done?

I've been trying to count the number of times a given word
appears in an ASCII file. This works for most terms:
==============================
my $search_term = shift;

my $count = 0;
while (<>) {
while (m/\b$search_this\b/g) {
$count++;
}
}
================================

use \Q

$ perl -n -e '\
BEGIN{$x=q($fn)} \
$n++ if /\Q$x/; \
END{print"n=$n\n"}' textfile
n=2

But it doesn't take input search terms beginning with $
such as $fn. I've tried

$./wordcount.pl \$fn textfile

and

$./wordcount.pl '\$fn' textfile

as well as

$./wordcount.pl $fn textfile


Which gives the message "undefined variable" or something like that.
Or the output says " textfile does not contain $fn" when it does.
That was for the '$fn' version. But 'fn' in @ARGV works just fine.
[The code lines with the result msgs have been snipped]

I cannot find anything about this in the Camel Book or the
Cookbook. BTW

$ grep '\$fn' textfile

finds all the lines with "$fn" in them, but I need an occurrence count.

grep's c option gives the count of matching lines
$ grep -c '$fn' textfile
2
 
T

Tad J McClellan

Tiro Verus said:
Can this be done?


Can what be done?

I've been trying to count the number of times a given word
appears in an ASCII file. This works for most terms: ^^^^^^^^^^
==============================
my $search_term = shift; ^^^^

my $count = 0;
while (<>) {
while (m/\b$search_this\b/g) { ^^^^
$count++;
}
}
================================


No it doesn't.

You should copy/paste working code rather than attempt to retype it.

Have you seen the Posting Guidelines that are posted here frequently?

But it doesn't take input search terms beginning with $
such as $fn.


Yes it does.

The real problem is with your pattern rather than with the search term.

Try this:

$_ = 'this has $fn in it';
print "matched boundary\n" if /\b\$fn/;
print "matched without\n" if /\$fn/;
print "matched BOUNDARY\n" if /\B\$fn/;

You should see that only the last two statements make output.

That is because \b requires either \w\W or \W\w and the string being
searched has \W\W (a space and a dollar sign).

This will make output:

$_ = 'this has$fn in it';
print "matched boundary\n" if /\b\$fn/;

because it has \w\W ("s" and dollar sign).

I've tried

$./wordcount.pl \$fn textfile

and

$./wordcount.pl '\$fn' textfile

as well as

$./wordcount.pl $fn textfile


How to quote things in the shell is a shell question, not a Perl question.

Which gives the message "undefined variable" or something like that.
^^^^^^^^^^^^^^^^^^^^^^

There is no such message.

Please copy/paste the verbatim text of messages.

Have you seen the Posting Guidelines that are posted here frequently?

Or the output says " textfile does not contain $fn" when it does.


That is because you have required a word boundary in your pattern.
 
T

Tad J McClellan

RedGrittyBrick said:
Tiro Verus wrote:

grep's c option gives the count of matching lines
$ grep -c '$fn' textfile


But that does not do what was asked for...

It will report 1 rather than 2 for

$fn is repeated as $fn on the same line
 
S

sln

Can this be done?

I've been trying to count the number of times a given word
appears in an ASCII file. This works for most terms:
==============================
my $search_term = shift;

my $count = 0;
while (<>) {
while (m/\b$search_this\b/g) {
$count++;
}
}
================================

But it doesn't take input search terms beginning with $
such as $fn. I've tried

$./wordcount.pl \$fn textfile

and

$./wordcount.pl '\$fn' textfile

as well as

$./wordcount.pl $fn textfile


Which gives the message "undefined variable" or something like that.
Or the output says " textfile does not contain $fn" when it does.
That was for the '$fn' version. But 'fn' in @ARGV works just fine.
[The code lines with the result msgs have been snipped]

I cannot find anything about this in the Camel Book or the
Cookbook. BTW

$ grep '\$fn' textfile

finds all the lines with "$fn" in them, but I need an occurrence count.
------------------------
textfile.txt:
------------------------
But it doesn't take input search terms beginning with $
such as $fn. I've tried

$./wordcount.pl \$fn textfile

and

$./wordcount.pl '\$fn' textfile

------------------------
wordcount.pl:
------------------------
use strict;
use warnings;

my ($search_term, $fname) = @ARGV;
my $count = 0;

open my $fh, $fname or die "Can't open $fname";
while (<$fh>) {
while (/$search_term/g) {
$count++;
}
}
close $fh;
print $count;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top