Solved

search for a special character in a file using perl script

Posted on 2011-02-18
4
514 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

825 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