Solved

search for a special character in a file using perl script

Posted on 2011-02-18
4
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34930675
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
ID: 34930700
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
ID: 34930760
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
ID: 34930871
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

735 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