How to replace a string in Perl for the given string?

Posted on 2012-08-16
Last Modified: 2012-08-21

Open in new window

How can I replace the html file name in the string given above as the below:

Open in new window

Question by:Tolgar
    LVL 5

    Expert Comment

    In sed u can use like the below

    In filename.txt file put the above text.

    sed 's/original/replace/g' filename.txt

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽


    Open in new window

    LVL 31

    Accepted Solution

    my $str1 = 'file://///dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11/dir12/20120816_1331_ReportDetails.html#/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11/dir12/file.c';
    my $str2 = 'file://///dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11/dir12/Test1_ReportDetails.html#/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11/dir12/file.c';
    my ($html) =  $str1 =~ /(\w+\.html)/;
    $str2 =~s /(\w+\.html)/$html/;
    print $str2;

    Open in new window


    Author Comment

    Original and replace are variables in my code. So I replaced single quotes with double quotes.

    This is my line of code:

    sed "s/$reportFileDetailsName/$newReportFileDetailsName/g" $reportFileName;

    Open in new window

    But got this error:

    String found where operator expected at test.t line 225, near "sed "s/$reportFileDetailsName/$newReportFileDetailsName/g""
    	(Do you need to predeclare sed?)
    Scalar found where operator expected at test.t line 225, near ""s/$reportFileDetailsName/$newReportFileDetailsName/g" $reportFileName"
    	(Missing operator before  $reportFileName?)
    syntax error at test.t line 225, near "sed "s/$reportFileDetailsName/$newReportFileDetailsName/g""

    Open in new window

    LVL 25

    Assisted Solution

    sed is a UNIX/Linux command ... not  a Perl function.

    try @farzanj's suggestion above or:

    $reportFileName =~ s/$reportFileDetailsName/$newReportFileDetailsName/g;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    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…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    761 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

    7 Experts available now in Live!

    Get 1:1 Help Now