Solved

search for a special character in a file using perl script

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

732 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