• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

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

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

Open in new window


How can I replace the html file name in the string given above as the below:
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

Open in new window

0
Tolgar
Asked:
Tolgar
2 Solutions
 
magentoCommented:
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

0
 
käµfm³d 👽Commented:
Try:

s#[^/]+_(ReportDetails.html)#Test1_$1#g

Open in new window

0
 
farzanjCommented:
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

0
 
TolgarAuthor Commented:
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

0
 
lwadwellCommented:
sed is a UNIX/Linux command ... not  a Perl function.

try @farzanj's suggestion above or:

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now