Using sed for change some parts of code

时间:2015-05-24 20:27:50

标签: bash sed

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

2 个答案:

答案 0 :(得分:1)

You can try with

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

答案 1 :(得分:0)

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