pattern replace inside text file using sed

Hi,

I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file.

For Example:

If a pattern found like

<a href="http://www.mysite.com/mypage.jsp">

it should be retained.

But if a pattern found like

<a href="../mypage.jsp">

it should be changed to

<a href="../mypage.html">

So the requirement is, if the URL is not starting with "http://" or "https://", change the file extension to ".html"

Any Ideas?

Thanks,
Meharo

try:

awk '{if($0 !~ /http/ && $0 !~ /https/){gsub(/jsp/,"html",$0);print}else print}' file
sed 's/\(.*="\..*\).\(jsp\)/\1.html/' file

awk '! /http[:s]/{sub(/jsp"/,"html\"")}1' file

thank you guys,

the pattern is not as simple as a single line or always the link starts with "../". It can be like

<a href="anypath/anypage.jsp">

or multiple links present in the same line like

<a href="anypath/anypage.jsp"><...><a href="http://www.mysite.com/anypath/anypage.jsp">

meharo

This code should solve this problem.

sed 's/\(.*=".*\).\(jsp\)/\1.html/' file

awk '! /http[:s]/{sub(/jsp"/,"html\"")}1' file

I didn't see this requirement on your original post :rolleyes: , maybe I should give you the chance to solve the problems by yourself.

Success.