简体   繁体   English

使用sed替换多个文件中的多行

[英]Using sed to replace multiple lines in multiple files

I have thousands of html files in which there are two different pieces of old google adsense code which needs to be replaced by new ones. 我有成千上万个html文件,其中有两个不同的旧Google adsense代码,需要用新代码替换。 The files are on a linux server. 这些文件位于Linux服务器上。

the old code looks like: 旧代码如下:

  <script type="text/javascript"><!--
google_ad_client = "pub-xxx";
google_ad_width = 336;
google_ad_height = 280;
google_ad_format = "336x280_as";
google_ad_type = "text";
//2007-06-09: Forum archive rectangle
google_ad_channel = "1013717472xx";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "000000";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

I need to match this text "google_ad_channel = "1013717472xx"" within the code, replace the whole code with new one. 我需要在代码中匹配此文本“ google_ad_channel =“ 1013717472xx”“,并用新代码替换整个代码。

the new code is as follows: 新代码如下:

<script src="https://pagead2.googlesyndication.com/pub-config/r20160913/ca-pub-xxx.js"></script><script src="http://pagead2.googlesyndication.com/pub-config/r20160913/ca-pub-xxx.js"></script><script type="text/javascript"><!--
google_ad_client = "pub-xxx";
/* frm arch linkunit 200x90, created 5/30/08 */
google_ad_slot = "0112084554";
google_ad_width = 200;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

How can I use sed to do the replacement in all the html files on my server? 如何使用sed替换服务器上所有html文件?

Thank you in advance! 先感谢您!

ls |xargs sed -i 's/google_ad_channel = "1013717472xx"/google_ad_channel = "new_code"/g'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM