Enric Agud Pique
Enric Agud Pique

Reputation: 1105

Using sed for change some parts of code

In a file htm I have several parts of the code with the following structure. It begins with infowindow.content= and finishes with ";

I need to change in these lines the first part infowind.content= for infowindow.setContent( and the last part
";
for
");

infowindow.content=" Poblacio : Berlin <br> Font : Metar <br> Data : 24.04.2015 - 15:24:05 <<br> T_Actual : 14 <br> T_Max : 20 <br> T_Min : 7 <br> Rain : 5 <br>";

infowindow.setContent(" Poblacio : Berlin <br> Font : Metar <br> Data : 24.04.2015 - 15:24:05  <br> T_Actual : 14 <br> T_Max : 20 <br> T_Min : 7 <br> Rain : 5 <br>");

I have changed the first part with the following code, but how can I modify also the last part of the code?.

sed -e "s/infowindow.content=/infowindow.setContent(" /home/htm/file.htm

Upvotes: 2

Views: 35

Answers (2)

josifoski
josifoski

Reputation: 1726

You can try with

sed -i '/content/ {s/content=/SetContent(/; s/;$/);/}' file  

Upvotes: 1

Hunter McMillen
Hunter McMillen

Reputation: 61512

You can add a capture group to "capture" all of the values between the = and the ;. Then use this captured value in your replace statement to perform the substitution all in one step:

sed -e "s/infowindow.content=\(.*\);/infowindow.setContent(\1);/" test.htm 

Upvotes: 0

Related Questions