【问题标题】:How to find and replace string with spaces for 8,000 plus HTML files如何为 8,000 多个 HTML 文件查找和替换带有空格的字符串
【发布时间】:2012-06-02 05:19:21
【问题描述】:

我想在我的 8,000 个(加)记录(html 文件)中执行查找和替换,但问题是我要替换的字符串包含空格。

我想查找所有具有</head> 实例的文件,以便在<head> 标记关闭之前插入我的谷歌分析。

我的 Google Analytics(分析)如下所示:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'XX-XXXXXXXX-X']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

找到&lt;/head&gt;后,结果应该是这样的:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'XX-XXXXXXXX-X']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>

在我的服务器上,我有 FTPSSH 访问权限。我应该使用什么方法来快速、轻量地替换它们?

谢谢。

【问题讨论】:

    标签: replace


    【解决方案1】:

    对所有 html 文件使用 sed

    sed 's/&lt;\/head&gt;/before end head/' input.html

    只需根据需要替换 'before end head'。不要忘记正确使用\

    【讨论】:

      【解决方案2】:

      你想要sed append 命令。

      【讨论】:

      • 感谢您的建议...您介意提供一些示例吗?我对命令不熟悉。
      猜你喜欢
      • 2016-03-02
      • 2013-03-05
      • 2013-03-25
      • 2014-03-01
      • 1970-01-01
      • 2019-10-18
      • 2014-09-05
      • 2015-09-22
      • 2013-02-18
      相关资源
      最近更新 更多