V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shayuvpn0001
V2EX  ›  程序员

关于使用正则表达式在多个文件中跨行替换的问题。

  •  
  •   shayuvpn0001 · 2019-01-20 11:47:48 +08:00 · 1909 次点击
    这是一个创建于 1895 天前的主题,其中的信息可能已经有所发展或是发生改变。

    接手一个老 ASP 项目,很老的那种 ASP 项目,引用了一批 js 和 css 文件,几百个 html 和 asp 混写的文件,没有把这些引用分离出去作为一个公用的 header,现在的问题是,原来他们引用的 js 和 css 文件服务器有一些要关闭了,所以干脆全部切换到自己的新服务器上。需要批量替换掉下面这种格式:

    <!---------------------------------------------------------------------->
    <!-- Glass theme CSS files, added by Jeff Mills, on April 16th, 2001 -->
    <link rel="stylesheet" type="text/css" href="http://www.abc.com/static/glass_theme.css">
    <!-- Dynamic Hue effect CSS files, added by Tom Brooks, on 05/12/2005 -->
    <link rel="stylesheet" type="text/css" href="http://www.def.com/include/css/dn.1.2.0.css">
    <!-- jQuery files, added by Mark Williams, on Jan. 27th, 2009 -->
    <!-- jQuery files, updated by Mark Williams, on Oct. 18th, 2010 -->
    <script src="http://www.jkl.uk/js/jquery.min.js"></script>
    

    先开始是想如果能够在原来注释下面继续修改并添加自己的就更好了,比如这样

    <!---------------------------------------------------------------------->
    <!-- Glass theme CSS files, added by Jeff Mills, on April 16th, 2001 -->
    <!-- Glass theme CSS files, updated by Y. Sha, on Jan. 18th, 2019 -->
    <link rel="stylesheet" type="text/css" href="http://www.newsvr.com/static/css/glass_theme.1.1.0.css">
    <!-- Dynamic Hue effect CSS files, added by Tom Brooks, on 05/12/2005 -->
    <!-- Dynamic Hue effect CSS files, updated by Y. Sha, on Jan. 18th, 2019 -->
    <link rel="stylesheet" type="text/css" href="http://www.newsvr.com/static/css/dn.1.2.0.css">
    <!-- jQuery files, added by Mark Williams, on Jan. 27th, 2009 -->
    <!-- jQuery files, updated by Mark Williams, on Oct. 18th, 2010 -->
    <!-- jQuery files, updated by Y. Sha, on Jan. 18th, 2019 -->
    <script src="http://www.newsvr.com/static/js/jquery.min.1.4.2.js"></script>
    

    我发现这样很麻烦,所以先把这个整体替换掉让项目能用,以后有时间再慢慢琢磨。

    所以决定干脆把这些统一替换成自己的:

    <!---------------------------------------------------------------------->
    <!-- Glass theme CSS files, updated by Y. Sha, on Jan. 18th, 2019 -->
    <link rel="stylesheet" type="text/css" href="http://www.newsvr.com/static/css/glass_theme.1.1.0.css">
    <!-- Dynamic Hue effect CSS files, updated by Y. Sha, on Jan. 18th, 2019 -->
    <link rel="stylesheet" type="text/css" href="http://www.newsvr.com/static/css/dn.1.2.0.css">
    <!-- jQuery files, updated by Y. Sha, on Jan. 18th, 2019 -->
    <script src="http://www.newsvr.com/static/js/jquery.min.1.4.2.js"></script>
    

    我用 grep -Pzo 使用 perl 格式的 pattern 可以找到这些内容,但是用 sed 去替换的时候就出现了问题。我对 sed 不熟,好象是 perl 格式写的正则无法直接在 sed 中使用。 以前经常用的多个文件单行内替换是这样操作的。

    grep 'pattern_old' -rl ./ | xargs sed -i "s/pattern_old/pattern_new/g"
    

    这条命令只适合多个文件的一行内使用,跨行就不行了,想请各位帮我参考一下,这个 sed 应该怎么写?

    8 条回复    2019-01-21 13:36:09 +08:00
    hcymk2
        1
    hcymk2  
       2019-01-20 13:30:18 +08:00
    DOM 建议用 DOM 工具来操作。
    AX5N
        2
    AX5N  
       2019-01-20 13:44:59 +08:00
    如果你只是改个 url 的话,单行多替换几次不也一样吗。
    ltux
        3
    ltux  
       2019-01-20 14:41:08 +08:00
    何不直接用 perl
    perl -p -0777 -e 's/old/new/g' FILE_PATH
    这结果会输出到终端,确认没问题了就加个 -i 选项完成文件替换
    perl -p -i -0777 -e 's/old/new/g' FILE_PATH
    1OF7G
        4
    1OF7G  
       2019-01-20 15:36:41 +08:00
    这是 Commit Log 写在注释里的节奏?不用 Git 之类的吗
    shayuvpn0001
        5
    shayuvpn0001  
    OP
       2019-01-20 16:18:43 +08:00
    @1OF7G 上个世纪的项目,你不能要求太高。

    @ltux 我刚在 stackoverflow 上看到了一个类似的解法,好像 sed 确实是不好弄,目前正在试 perl。
    alcarl
        6
    alcarl  
       2019-01-20 17:07:16 +08:00 via Android
    3 楼的方法应该可行,改一下正则试试吧
    ltux
        7
    ltux  
       2019-01-20 19:17:28 +08:00
    如果你用了元字符 . 并希望他能匹配换行符,则需要 /s 开关,即 dotall 模式。
    选项 -0777 的意思是一次性读入整个文件。无此选项则一次读一行,你的替换操作也仅限于此行。
    vincentxue
        8
    vincentxue  
       2019-01-21 13:36:09 +08:00 via iPhone
    你点我资料看一下我开源那个正则项目里有 sed 和 awk 的正则,和首页的那些 pcre 的对比一下就知道区别了,主要就是转义和一些高级语法支持的问题。sed 用的是 posix 标准,和 pcre 不兼容的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5418 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:46 · PVG 14:46 · LAX 23:46 · JFK 02:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.