Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

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

Avatar of magento
magento

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

Try:

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial