AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 772527
Accepted
alancc
alancc
Asked: 2024-03-17 12:30:44 +0800 CST2024-03-17 12:30:44 +0800 CST 2024-03-17 12:30:44 +0800 CST

sed 在文件中替换,同时使用文件中的旧字符串和新字符串

  • 772

我想自动注释掉PHP文件中的代码块,如下所示:

原始块:

    //  Enable all errors
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

带注释的新块:

    /* For the production version, the following codelines are commented
       out
    //  Enable all errors
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    */

所以我打算将这些行放在两个文件中,并使用 sed 自动执行替换。然而,在网上搜索后,我只找到Replace string with content of a file using sed和sed - Replace string with file content,这意味着要么只有源模式或目标模式在一个文件中,其余的在网上。但文件中没有两者的样本。

那么,如何进行更换呢?我应该使用 sed 还是 awk?

awk
  • 3 3 个回答
  • 73 Views

3 个回答

  • Voted
  1. aviro
    2024-03-17T19:14:59+08:002024-03-17T19:14:59+08:00

    我建议您使用该patch实用程序来实现此目的。

    diff1.使用命令创建补丁文件

    假设您有两个文件,其中一个包含要替换的块:

    $ cat toreplace.txt 
        //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
    

    另一个包含您要替换的块:

    $ cat replacewith.txt 
        /* For the production version, the following codelines are commented
           out
        //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
        */
    

    您在它们之间创建上下文差异并将内容放入补丁文件中:

    $ diff -c toreplace.txt replacewith.txt > patchfile
    $ cat patchfile
    *** toreplace.txt       2024-03-17 12:12:31.073270945 +0200
    --- replacewith.txt     2024-03-17 12:12:45.276887865 +0200
    ***************
    *** 1,4 ****
    --- 1,7 ----
    +     /* For the production version, the following codelines are commented
    +        out
          //  Enable all errors
          ini_set('display_startup_errors', 1);
          ini_set('display_errors', 1);
          error_reporting(E_ALL);
    +     */
    

    2. 应用补丁

    现在考虑这是原始文件:

    $ cat myfile
    line before 1
    line before 2
    line before 3
        //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
    line after 1
    line after 2 
    line after 3
    

    您可以使用之前创建的patch文件对要更改的文件运行命令。patchfile

    $ patch -cb myfile patchfile
    patching file myfile
    Hunk #1 succeeded at 4 (offset 3 lines).
    
    • 标志“-c将补丁文件解释为上下文差异(指定或选项时实用程序 diff 的输出)”。-c-C。
      • 它不是严格必需的,因为没有它“patch实用程序将尝试确定差异列表的类型,除非被-c、-e或-n选项否决。”
    • -b“在应用差异之前,将每个修改文件的原始内容的副本保存在同名的文件中并附加后缀”选项。.orig
      • 如果您不想创建备份,可以删除此标志。

    3. 验证

    现在将原始文件与修补后的文件进行比较:

    $ diff -c myfile{.orig,}
    *** myfile.orig 2024-03-17 13:00:24.936142831 +0200
    --- myfile      2024-03-17 13:13:48.882669202 +0200
    ***************
    *** 1,10 ****
    --- 1,13 ----
      line before 1
      line before 2
      line before 3
    +     /* For the production version, the following codelines are commented
    +        out
          //  Enable all errors
          ini_set('display_startup_errors', 1);
          ini_set('display_errors', 1);
          error_reporting(E_ALL);
    +     */
      line after 1
      line after 2 
      line after 3
    
    • 2
  2. Ed Morton
    2024-03-18T03:09:54+08:002024-03-18T03:09:54+08:00

    不要尝试使用sed它,因为sed不理解文字字符串(请参阅is-it-possible-to-escape-regex-metacharacters-reliously-with-sed),使用类似的工具awk确实理解文字字符串。

    使用 GNUawk进行多字符RS和ARGIND:

    $ awk -v RS='^$' -v ORS= '
        ARGIND < 3 { a[ARGIND]=$0; next }
        s = index($0,a[1]) {
            $0 = substr($0,1,s-1) a[2] substr($0,s+length(a[1]))
        }
        { print }
    ' old new file
    this is
    the winter
        /* For the production version, the following codelines are commented
           out
        //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
        */
    of our
    discontent
    

    或使用任何awk:

    $ awk '
        FNR == 1 { a[++argind]=$0; next }
        { a[argind] = a[argind] ORS $0 }
        END {
            $0 = a[3]
            if ( s = index($0,a[1]) ) {
                $0 = substr($0,1,s-1) a[2] substr($0,s+length(a[1]))
            }
            print
        }
    ' old new file
    this is
    the winter
        /* For the production version, the following codelines are commented
           out
        //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
        */
    of our
    discontent
    

    以上是使用这些输入文件运行的:

    $ head old new file
    ==> old <==
        //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
    
    ==> new <==
        /* For the production version, the following codelines are commented
           out
        //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
        */
    
    ==> file <==
    this is
    the winter
        //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
    of our
    discontent
    
    • 2
  3. Best Answer
    Praveen Kumar BS
    2024-03-19T20:27:48+08:002024-03-19T20:27:48+08:00
     sed -e '1s/.*/\/* For the production version, the following codelines are commented\nout\n&/g'  -e '$s/.*/&\n*\//g'filename
    
    
    output
    
    /* For the production version, the following codelines are commented
    out
      //  Enable all errors
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
    */
    
    • 1

相关问题

  • 根据第一个逗号之前的匹配删除重复行数

  • 在另一个文件之后逐行追加行

  • 如何删除两行之间的单行

  • 重新排列字母并比较两个单词

  • 多行文件洗牌

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve