Solved

search for a special character in a file using perl script

Posted on 2011-02-18
4
493 Views
Last Modified: 2012-08-14
I want to search and replace a string in a file using a perl script and this string contains a special character like $xyz and replace it with a special string but i am currently having issues with it. Can someone please telling me what is going on?
#!/usr/bin/perl
use File::Copy;
# usage:   perl crHtmlFile 

$oldlat = "$xyz"; $newstr = "newstring";

$FILE1 = "myfile";

open(FILE1) or die "Could not read from $filename, program halting. $!";

# loop through the input file
while (<FILE1>)
{
  # get the current record
  chomp;
  # here i want to replace every occurrence of "$xyz" with "newstring" but perl does not find "$xyz" and i suspect its because of the special character $.
  $_ =~ s/$oldstr/$newstr/;
 
}

Open in new window

0
Comment
Question by:Atouray
  • 3
4 Comments
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
For starters, on line 5, use single quotes instead of double quotes to keep PERL from interpreting the dollar sign as the start of a variable name.  Alternatively, you could use "\$xyz" or q{$xyz}.

$oldlat = '$xyz'; $newstr = "newstring";

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
and you need to use \Q and \E in the replace:

$_ =~ s/\Q$oldlat\E/$newstr/;

Open in new window


Also, is the pattern variable supposed to be called "$oldlat" or "$oldstr".  Pick one.  :)
0
 

Author Comment

by:Atouray
Comment Utility
the pattern variable is $oldstr. I have tried single quotes and \Q, \E but it does not help
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
Comment Utility
Try this:

#!/usr/bin/perl
use File::Copy;
# usage:   perl crHtmlFile 

$oldstr = '$xyz'; $newstr = "newstring";

$FILE1 = "myfile";

open(FILE1) or die "Could not read from $filename, program halting. $!";

# loop through the input file
while (<FILE1>)
{
  # get the current record
  chomp;
  # here i want to replace every occurrence of "$xyz" with "newstring" but perl does not find "$xyz" and i suspect its because of the special character $.
  $_ =~ s/\Q$oldstr\E/$newstr/;
 
  print "$_\n";
}

Open in new window


test.pl

$oldstr = '$xyz'; $newstr = "newstring";

$_ = 'foo bar $xyz gronk';
print "$_\n"; 
$_ =~ s/\Q$oldstr\E/$newstr/;
print "$_\n";

Open in new window


Results:

c:\>perl c:\temp\test.pl
foo bar $xyz gronk
foo bar newstring gronk

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now