【问题标题】:'sed' : How to add new line after string match + 2 lines'sed' : 如何在字符串匹配 + 2 行后添加新行
【发布时间】:2013-04-25 12:29:54
【问题描述】:

我想在字符串匹配 + 2 行之后添加一个新行。

这是我的文件:

allow-hotplug eth0
auto eth0
iface eth0 inet static
address 172.16.2.245
netmask 255.255.254.0
gateway 192.168.1.1

allow-hotplug eth1
#auto eth1
iface eth1 inet static
address 192.168.0.240
netmask 255.255.255.0

iface eth2 inet static
address 192.168.2.240
netmask 255.255.255.0

我想在找到 'iface eth1' + 2 行后添加 'gateway 192.168.1.1'。

示例:执行 sed 命令后我需要得到什么

allow-hotplug eth0
auto eth0
iface eth0 inet static
address 172.16.2.245
netmask 255.255.254.0
gateway 172.16.2.254

allow-hotplug eth1
#auto eth1
iface eth1 inet static
address 192.168.0.240
netmask 255.255.255.0
gateway 192.168.1.1

iface eth2 inet static
address 192.168.2.240
netmask 255.255.255.0

我知道如何在特定字符串之后查找和移动 2 行,在特定字符串之后附加行等,但不结合这 2 个操作。 斯蒂芬

【问题讨论】:

    标签: sed


    【解决方案1】:

    这似乎有效:

    sed '/^iface eth1/{N;N;s/$/\ngateway 192.168.1.1/}' input.txt
    

    -i 选项添加到sed 以将结果保存回input.txt

    【讨论】:

    • 非常感谢!这真的很有帮助。
    • @user2319609 很高兴能帮上忙。顺便说一句,你可以accept the answer
    • 很棒的答案。将经常使用它,因为我曾经做过类似line_num2=$(grep -n 'txt_to_find' targ_file.h | awk '{print $1}') 的操作,然后添加以获取我需要的行号!这样效率更高。
    • @LevLevitsky 感谢您的解决方案。在这种情况下,如果要附加的文本是一个带有斜杠的变量(例如路径),您将如何更改分隔符?谢谢
    • @LevLevitsky 更新:发现我可以在内部 s/// 语句中使用逗号,例如 sed '/^iface eth1/{n;n;s,$,\n'"$ PATH_VAR"',}'
    【解决方案2】:

    您要求使用“sed”,但“Kent”正在使用“awk”,这是一个 sed 脚本,可以为您的示例执行您想要的操作。更一般地说,sed 脚本的第 1 行可以包含您想要的任何字符串,而 sed 脚本的第 5 行可以包含您想要的任何字符串。将以下脚本放在一个文件中,比如 x.sed,不要添加任何空格或制表符。

        /iface eth1/{
        n
        n
        a\
        gateway 192.168.1.1
        }
    

    然后在命令行上这样运行。

        sed -f x.sed "myinputfile" > "myoutputfile"
    

    【讨论】:

    • 谢谢,我也尝试过,像这样... sed -i.bak '/^iface eth1/{n;n;a\gateway 192.168.1.1}' /etc/network /接口
    • ...没有成功!!??不知道为什么?
    【解决方案3】:

    一种使用sed的方式:

    sed '
    /iface eth1/ {
    n
    n
    a\gateway 192.168.1.1
    }' file
    

    【讨论】:

      【解决方案4】:

      如果你想在该块的末尾添加一行,试试这个:

      awk -v RS="" -v ORS="\n\n" '/iface eth1/{$0=$0"\ngateway 192.168.1.1"}1' file 
      

      使用您的文件:

      kent$  cat file
      allow-hotplug eth0
      auto eth0
      iface eth0 inet static
      address 172.16.2.245
      netmask 255.255.254.0
      gateway 192.168.1.1
      
      allow-hotplug eth1
      #auto eth1
      iface eth1 inet static
      address 192.168.0.240
      netmask 255.255.255.0
      
      iface eth2 inet static
      address 192.168.2.240
      netmask 255.255.255.0
      
      kent$  awk -v RS="" -v ORS="\n\n" '/iface eth1/{$0=$0"\ngateway 192.168.1.1"}1' file
      allow-hotplug eth0
      auto eth0
      iface eth0 inet static
      address 172.16.2.245
      netmask 255.255.254.0
      gateway 192.168.1.1
      
      allow-hotplug eth1
      #auto eth1
      iface eth1 inet static
      address 192.168.0.240
      netmask 255.255.255.0
      gateway 192.168.1.1
      
      iface eth2 inet static
      address 192.168.2.240
      netmask 255.255.255.0
      

      【讨论】:

        猜你喜欢
        • 2012-01-09
        • 2011-04-05
        • 1970-01-01
        • 2020-12-23
        • 2017-02-13
        • 1970-01-01
        • 1970-01-01
        • 2021-09-17
        • 1970-01-01
        相关资源
        最近更新 更多