shell脚本 - 在文件中搜索和追加内容

时间:2018-03-15 17:59:36

标签: shell sed

我有一个名为“menu.lst”的示例文件,如下所示,

 1  rsu_mirror
 2      root (hd0,0)
 3      kernel /vmlinuz-3.0.101-100-default root=/dev/system/root console=tty0 resume=/dev/system/swap splash=silent crashkernel= showopts panic=1 numa=off noht elevator=deadline
 4      initrd /initrd-3.0.101-100-default
 5
 6  rsu_primary
 7      root (hd0,0)
 8      kernel /vmlinuz-3.0.101-100-default root=/dev/system/root console=tty0 resume=/dev/system/swap splash=silent crashkernel= showopts panic=1 numa=off noht elevator=deadline
 9      initrd /initrd-3.0.101-100-default

我需要首先找到“rsu_mirror”和“rsu_primary”之间的“内核”行(从上面o / p它是第3行)然后在 NEW_PARAMETER 的末尾添加 NEW_PARAMETER 线。

所以我的最终输出应如下所示(差异在第3行,附加 NEW_PARAMETER ),

 1  rsu_mirror
 2      root (hd0,0)
 3      kernel /vmlinuz-3.0.101-100-default root=/dev/system/root console=tty0 resume=/dev/system/swap splash=silent crashkernel= showopts panic=1 numa=off noht elevator=deadline **NEW_PARAMETER**
 4      initrd /initrd-3.0.101-100-default
 5
 6  rsu_primary
 7      root (hd0,0)
 8      kernel /vmlinuz-3.0.101-100-default root=/dev/system/root console=tty0 resume=/dev/system/swap splash=silent crashkernel= showopts panic=1 numa=off noht elevator=deadline
 9      initrd /initrd-3.0.101-100-default

提前致谢

1 个答案:

答案 0 :(得分:0)

NEW_PARAMETER追加到menu.lst第3行的末尾:

$ sed -i '3s/$/NEW_PARAMETER/' menu.lst

在:

$ cat menu.lst
rsu_mirror
    root (hd0,0)
    kernel /vmlinuz-3.0.101-100-default root=/dev/system/root console=tty0 resume=/dev/system/swap splash=silent crashkernel= showopts panic=1 numa=off noht elevator=deadline
    initrd /initrd-3.0.101-100-default

rsu_primary
    root (hd0,0)
    kernel /vmlinuz-3.0.101-100-default root=/dev/system/root console=tty0 resume=/dev/system/swap splash=silent crashkernel= showopts panic=1 numa=off noht elevator=deadline
    initrd /initrd-3.0.101-100-default

后:

$ sed -i '3s/$/NEW_PARAMETER/' menu.lst
$ cat menu.lst
rsu_mirror
    root (hd0,0)
    kernel /vmlinuz-3.0.101-100-default root=/dev/system/root console=tty0 resume=/dev/system/swap splash=silent crashkernel= showopts panic=1 numa=off noht elevator=deadline
NEW_PARAMETER
    initrd /initrd-3.0.101-100-default

rsu_primary
    root (hd0,0)
    kernel /vmlinuz-3.0.101-100-default root=/dev/system/root console=tty0 resume=/dev/system/swap splash=silent crashkernel= showopts panic=1 numa=off noht elevator=deadline
    initrd /initrd-3.0.101-100-default