【问题标题】:find a pattern in html and replace it with php code在 html 中找到一个模式并将其替换为 php 代码
【发布时间】:2016-12-17 15:28:31
【问题描述】:

我正在寻找这种模式

<!-- Footer part at bottom of page-->
<div id="footer">
   <div class="row col-md-2 col-md-offset-5">

    <p class="text-muted">&copy; 2014. Core Team</p>
  </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div>

并用这种模式替换许多 .html 文件

<!-- Footer part at bottom of page-->
<div id="footer">
    <div class="row col-md-2 col-md-offset-5">
       <?php
            $year = date("Y");
            echo "<p class='text-muted'>© $year. Core Team</p>";
        ?>
    </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div>

注意区别在于 这个

<p class="text-muted">&copy; 2014. Core Team</p>

替换为

       <?php
            $year = date("Y");
            echo "<p class='text-muted'>© $year. Core Team</p>";
        ?>

我正在考虑使用sed 来做这件事,但在进行了初步尝试后,我的困难在于我可能会或可能会或可能不必逃脱的角色。还有 php 代码中的制表符或新行,我希望它在这里显示。

有很多文件要做,所以我想自动化它,但手动(复制和粘贴)可能会更快。但在这种情况下,sed 可能是错误的方法。有人可以指导我正确的方向吗?在这个阶段,我愿意接受其他语言(例如 php、python、bash )来寻找解决方案。

然后,我计划将每个 .html 文件重命名为 .php,如下所示:

for i in *.html; do mv "$i" "${i%.*}.php"; done;

编辑1

基于下面的 awk 答案,我可以让它在这个版本下工作

$ awk -Wversion 2>/dev/null || awk --version
GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.2, GNU MP 6.0.0)
Copyright (C) 1989, 1991-2014 Free Software Foundation.

但是在这个版本上我得到不同的输出。它似乎打印出 3 个文件,旧的新文件和文件。 这个版本容易纠正吗?

root@4461f768e343:/github/find_pattern# awk -Wversion 2>/dev/null || awk --version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

root@4461f768e343:/github/find_pattern#
root@4461f768e343:/github/find_pattern#
root@4461f768e343:/github/find_pattern# awk -v RS='^$' -v ORS= 'ARGIND==1{old=$0;next} ARGIND==2{new=$0;next} s=index($0,old){ $0 = substr($0,1,s-1) new substr($0,s+length(old))} 1' old new file
<!-- Footer part at bottom of page-->
<div id="footer">
   <div class="row col-md-2 col-md-offset-5">

    <p class="text-muted">&copy; 2014. Core Team</p>
  </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div><!-- Footer part at bottom of page-->
<div id="footer">
    <div class="row col-md-2 col-md-offset-5">
       <?php
            $year = date("Y");
            echo "<p class='text-muted'>© $year. Core Team</p>";
        ?>
    </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div>some pile of text
or other
<!-- Footer part at bottom of page-->
<div id="footer">
   <div class="row col-md-2 col-md-offset-5">

    <p class="text-muted">&copy; 2014. Core Team</p>
  </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div>
and more maybe.root@4461f768e343:/github/find_pattern#

【问题讨论】:

标签: php python bash sed


【解决方案1】:

您可以使用replace

html_files = ['a.html', ...]
copyright = '<p class="text-muted">&copy; 2014. Core Team</p>'
new_copyright = """       <?php
        $year = date("Y");
        echo "<p class='text-muted'>© $year. Core Team</p>";
    ?>"""
for html_file_path in html_files:
    with open(html_file_path) as html_file:
        html = html_file.read()

    if copyright in html:
        php_file_path = html_file_path.replace('.html', '.php')
        with open(php_file_path, "w") as php_file:
            php = html.replace(copyright, new_copyright)
            php_file.write(php)

请注意,这不会覆盖您的 html 文件,这在脚本有错误时很有用。

【讨论】:

  • 试过了,但得到了这个$ python find_pattern.py File "find_pattern.py", line 5 SyntaxError: Non-ASCII character '\xc2' in file find_pattern.py on line 6, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
  • tks。必须在文件顶部添加# -*- coding: utf-8 -*- this
【解决方案2】:

sed 用于单个行的简单替换,因此您的任务肯定不是 sed 的工作。如果您的文件格式都很好,您可以使用 awk:

$ cat old
<!-- Footer part at bottom of page-->
<div id="footer">
   <div class="row col-md-2 col-md-offset-5">

    <p class="text-muted">&copy; 2014. Core Team</p>
  </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div>

.

$ cat new
<!-- Footer part at bottom of page-->
<div id="footer">
    <div class="row col-md-2 col-md-offset-5">
       <?php
            $year = date("Y");
            echo "<p class='text-muted'>© $year. Core Team</p>";
        ?>
    </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div>

.

$ cat file
some pile of text
or other
<!-- Footer part at bottom of page-->
<div id="footer">
   <div class="row col-md-2 col-md-offset-5">

    <p class="text-muted">&copy; 2014. Core Team</p>
  </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div>
and more maybe.

.

$ awk -v RS='^$' -v ORS= 'ARGIND==1{old=$0;next} ARGIND==2{new=$0;next} s=index($0,old){ $0 = substr($0,1,s-1) new substr($0,s+length(old))} 1' old new file
some pile of text
or other
<!-- Footer part at bottom of page-->
<div id="footer">
    <div class="row col-md-2 col-md-offset-5">
       <?php
            $year = date("Y");
            echo "<p class='text-muted'>© $year. Core Team</p>";
        ?>
    </div>

    <div id="downloadlinks">
    <!-- downloadlinks go here-->
    </div>
</div>
and more maybe.

以上使用 GNU awk 处理多字符 RS 和 ARGIND。如果您想对许多文件执行此操作,您可以使用:

find . -type f -name '*.php' -exec awk -i inplace -v RS='^$' -v ORS= 'ARGIND==1{old=$0;print;next} ARGIND==2{new=$0;print;next} s=index($0,old){ $0 = substr($0,1,s-1) new substr($0,s+length(old))} 1' old new {} \;

或类似的。

【讨论】:

  • 这对我来说有点高级 awk,但 tks 目前正在进一步研究 RS ORS 和 ARGIND herehere
  • 1-做多个文件我只需要oldnew文件吗?
  • 不要相信随机网站获取信息,请阅读 Arnold Robbins 的《Effective Awk Programming, 4th Edition》一书。 1. 旧文件包含您要查找的字符串,新文件包含您要替换的字符串。之后,您列出要在其中搜索的文件。 2. 极端情况下,它是在进行字符串比较,因此所有字符在每个位置都必须相同。 3.它将old文件的内容读入一个名为old的变量,然后将new文件的内容读入new,然后在每个文件中搜索old的值并打印它前面的字符串,然后new,然后是它后面的字符串。
  • 是的,但是既然你有傻瓜,为什么还要麻烦呢?反正我加了一个例子。
  • 是的,它需要更多的工作 - 您需要为每个文件一次建立一行记录,而不仅仅是最后一个。或者你可以选择你知道不会出现在 RS 文件中的控制字符而不是 ^$ 并坚持更接近 gawk 代码,有些人依赖 RS='\0' 但 YMMV。我将删除该示例,因为我对尝试使其在非 gawk 中工作不感兴趣,因为它不像我最初想象的那么简单。如果您尝试过并且有任何具体问题,请随时发布后续信息,我会尽力为您提供帮助。
猜你喜欢
  • 2014-09-07
  • 1970-01-01
  • 2019-11-28
  • 2013-10-23
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 2016-07-04
  • 1970-01-01
相关资源
最近更新 更多