Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Substitution with s///

Posted on 2006-04-09
2
Medium Priority
?
199 Views
Last Modified: 2010-03-05
Hi Experts,

 I am using the s/// patern to substitute a source string with a target string.
I am getting the source string and the target string in 2 variables as below.
my $searchpattern = <STDIN>;
my $replacestring = <STDIN>;

I am looping through a file as below and trying to replace sourcepattern with replacestring as below and writing into another file.

foreach (<IN>){

      s/"$searchpattern"/"$replacestring"/;
      print OUT $line;
}

the above is not working when I am using the variables . It works if I replace(given directly the source and target strings) the above syntax as below.

foreach (<IN>){

      s/saw/was/;
      print OUT $line;
}


Please let me know where I am going wrong/ what is the correct way to do that.

Thanks in advance. I need this very urgently.
0
Comment
Question by:nmretd
  • 2
2 Comments
 
LVL 28

Accepted Solution

by:
FishMonger earned 2000 total points
ID: 16412160
Try this:

chomp(my $searchpattern = <STDIN>);
chomp(my $replacestring = <STDIN>);

foreach (<IN>){

     s/$searchpattern/$replacestring/;
     print OUT $line;
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 16412169
Oops, one more adjustment.  The regex is being applied to $_ but you're printing $line and $line is not being assigned.

foreach (<IN>){

     s/$searchpattern/$replacestring/;
     print OUT;
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans
Suggested Courses

572 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