Substitution with s///

Posted on 2006-04-09
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>){

      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>){

      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.
Question by:nmretd
    LVL 28

    Accepted Solution

    Try this:

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

    foreach (<IN>){

         print OUT $line;
    LVL 28

    Expert Comment

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

    foreach (<IN>){

         print OUT;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    remove duplicates from the csv file 13 86
    iSeries PERL Scripts 7 117
    Any syntax error for this clone.plscript 6 117
    PERL - Find newest folder 12 86
    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…
    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…
    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…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    737 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

    16 Experts available now in Live!

    Get 1:1 Help Now