How to Grep for the Dollar Symbol ($)

How to grep for the dollar symbol ($)?

The problem is that the shell expands variable names inside double-quoted strings. So for "$$$" it tries to read a variable name starting with the first $.

In single quotes, on the other hand, variables are not expanded. Therefore, '$$$' would work – if it were not for the fact that $ is a special character in regular expressions denoting the line ending. So it needs to be escaped: '\$\$\$'.

Searching file containing dollar sign $ with grep

Please see this answer in another stack exchange forum. Basically you should try grep '\$'.

Escape dollar sign in regexp for sed

There are other problems with your script, but file names containing $ are not a problem if you properly quote the argument to rm in the resulting script.

echo "rm -f '$i'" >> REMOVEOLDFILES.sh

or using printf, which makes quoting a little nicer and is more portable:

printf "rm -f '%s'" "$i" >> REMOVEOLDFILES.sh

(Note that I'm addressing the real problem, not necessarily the question you asked.)

In Makefiles, Is there a way to print the dollar sign ($) using @echo or $(info )?

Double the dollar sign to stop make from interpreting it

echo $$
$(info $$)

Matching whole word that includes a dollar sign?


Regex

\[\$myString]

Regular expression visualization

Debuggex Demo

C#

string pattern = @"\[\$" + myString + @"]";

Description

\[\$myString]
\[ matches the character [ literally
\$ matches the character $ literally
myString matches the characters myString literally (case sensitive)
] matches the character ] literally

Matching Dollar Sign in Perl String

This code

if ($foo =~ /(.*?)(\$\d+(?:\.\d+)?)/) {
print "match1 is $1, match2 is $2, match3 is $3, match4 is $4\n";
}

With this input

Vanilla Cake $3.65 

Will print

Use of uninitialized value $3 in concatenation (.) or string at ...
Use of uninitialized value $4 in concatenation (.) or string at ...
match1 is Vanilla Cake , match2 is $3.65, match3 is , match4 is

The warnings will be silent if you do not have use warnings enabled.

This is what the code you have supplied does with this input. You also show that it does with your screenshot. You say, in comments, that it does not do this on your home PC. I would say that is impossible.

Either your code is different, your input is different, or your Perl installation is different (although this is unlikely the issue). There is really no alternative.

One huge problem is that you are not using use strict; use warnings with your code. That can mean that any number of problems with your code are hidden. Most likely, in your case, I would say it is a typo, such as:

$Iine = $_;
if ($line =~ /...../) # <---- not the same variable

But you asked for 8 hours to update your code, so I guess we will find out in 8 hours.


A few pointers

  while (<$fh>)
{
$line=''; #Initialize the line variable
$line=$_; #Reading a record from a text file
  • You do not need to "initialize" the line variable. The next line will make that line completely redundant.
  • That line is not actually reading a record from your file, the readline statement <$fh> is doing that.
  • Usually you would write this line as: while (my $line = <$fh>).
  • $3 and $4 in your print statement can never hold a value, because you lack the capture groups ( ... ) necessary. Two capture groups means only $1 and $2 will be populated.

When writing Perl code, you should always use

use strict;
use warnings;

Because not doing so will not help you, it will just hide your problems.

Also make a habit of placing the declaration (my $var) in as small a scope as possible. Sample code:

use strict;
use warnings;
use feature 'say';

while (my $line = <DATA>) {
my @x = split /\|/, $line;
if ($x[0] =~ /(.*?)(\$\d+(?:\.\d+)?)/) {
say "$1 is $2";
}
}

__DATA__
Vanilla Cake $3.65 New Offering|Half pound Vanilla Cake||Cake with vanilla, cream and cheese

How to escape dollar sign ($) in a string using perl regex

Try this:

my %special_characters;
$special_characters{"_"} = "\\_";
$special_characters{"\\\$"} = "\\\$";
$special_characters{"{"} = "\\{";
$special_characters{"}"} = "\\}";
$special_characters{"#"} = "\\#";
$special_characters{"%"} = "\\%";
$special_characters{"&"} = "\\&";

Looks weird, right? Your regex needs to look as follows:

s/\$/\$/g

In the first part of the regex, "$" needs to be escaped, because it's a special regex character denoting the end of the string.

The second part of the regex is considered as a "normal" string, where "$" doesn't have a special meaning. Therefore the backslash is a real backslash whereas in the first part it's used to escape the dollar sign.

Furthermore in the variable definition you need to escape the backslash as well as the dollar sign, because both of them have special meaning in double-quoted strings.



Related Topics



Leave a reply



Submit