Solved

search for a special character in a file using perl script

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl script to parse xml file 2 104
rename outfile before writing 2 71
batch script for automated email 12 101
To run CGI/perl on tomcat and connect to oracle database. 5 118
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

20 Experts available now in Live!

Get 1:1 Help Now