2017-09-03 101 views
1

任何人都可以帮助我在行尾添加条目。shell脚本编辑和添加文件中的条目

这是linux操作系统。

下面是该文件的内容/boot/grub2/grub.cfg

### BEGIN /etc/grub.d/10_linux ### 
menuentry 'Red Hat Enterprise Linux Server (3.10.0-327.el7.x86_64) 7.2 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f' { 
     load_video 
     set gfxpayload=keep 
     insmod gzio 
     insmod part_msdos 
     insmod xfs 
     set root='hd0,msdos1' 
     if [ x$feature_platform_search_hint = xy ]; then 
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 0ee3bceb-d7b7-4e56-ae16-a17ed5f395ea 
     else 
      search --no-floppy --fs-uuid --set=root 0ee3bceb-d7b7-4e56-ae16-a17ed5f395ea 
     fi 
     linux16 /vmlinuz-3.10.0-327.el7.x86_64 root=UUID=75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8 
     initrd16 /initramfs-3.10.0-327.el7.x86_64.img 
} 
menuentry 'Red Hat Enterprise Linux Server (0-rescue-3b0570b9c0c44fd7b8426ed7d80f55ff) 7.2 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-3b0570b9c0c44fd7b8426ed7d80f55ff-advanced-75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f' { 
     load_video 
     insmod gzio 
     insmod part_msdos 
     insmod xfs 
     set root='hd0,msdos1' 
     if [ x$feature_platform_search_hint = xy ]; then 
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 0ee3bceb-d7b7-4e56-ae16-a17ed5f395ea 
     else 
      search --no-floppy --fs-uuid --set=root 0ee3bceb-d7b7-4e56-ae16-a17ed5f395ea 
     fi 
     linux16 /vmlinuz-0-rescue-3b0570b9c0c44fd7b8426ed7d80f55ff root=UUID=75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f ro crashkernel=auto rhgb quiet 
     initrd16 /initramfs-0-rescue-3b0570b9c0c44fd7b8426ed7d80f55ff.img 
} 

我需要在该行汇编“text1”和“text2”中的末尾添加条目。

linux16 /vmlinuz-3.10.0-327.el7.x86_64 root=UUID=75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8 text1 text2 

编辑后,我的文件应该是这样:

### BEGIN /etc/grub.d/10_linux ### 
menuentry 'Red Hat Enterprise Linux Server (3.10.0-327.el7.x86_64) 7.2 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f' { 
     load_video 
     set gfxpayload=keep 
     insmod gzio 
     insmod part_msdos 
     insmod xfs 
     set root='hd0,msdos1' 
     if [ x$feature_platform_search_hint = xy ]; then 
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 0ee3bceb-d7b7-4e56-ae16-a17ed5f395ea 
     else 
      search --no-floppy --fs-uuid --set=root 0ee3bceb-d7b7-4e56-ae16-a17ed5f395ea 
     fi 
     linux16 /vmlinuz-3.10.0-327.el7.x86_64 root=UUID=75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8 text1 text2 
     initrd16 /initramfs-3.10.0-327.el7.x86_64.img 
} 
menuentry 'Red Hat Enterprise Linux Server (0-rescue-3b0570b9c0c44fd7b8426ed7d80f55ff) 7.2 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-3b0570b9c0c44fd7b8426ed7d80f55ff-advanced-75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f' { 
     load_video 
     insmod gzio 
     insmod part_msdos 
     insmod xfs 
     set root='hd0,msdos1' 
     if [ x$feature_platform_search_hint = xy ]; then 
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 0ee3bceb-d7b7-4e56-ae16-a17ed5f395ea 
     else 
      search --no-floppy --fs-uuid --set=root 0ee3bceb-d7b7-4e56-ae16-a17ed5f395ea 
     fi 
     linux16 /vmlinuz-0-rescue-3b0570b9c0c44fd7b8426ed7d80f55ff root=UUID=75a4fe9b-ade6-4db0-a19c-e2dce64b4f2f ro crashkernel=auto rhgb quiet 
     initrd16 /initramfs-0-rescue-3b0570b9c0c44fd7b8426ed7d80f55ff.img 
} 
+5

你能不能拿出一个更最小(如[mcve])例如,输入和输出之间的差异会更清晰吗? –

+0

这[问题和答案](https://stackoverflow.com/questions/9591744/add-to-the-end-of-a-line-containing-a-pattern-with-sed-or-awk)是相关的。 – Mox

+0

@all,我想提供更多的信息.​​.建议我会改进和提供最低限度的信息。 –

回答

2

随着sed的表达:

sed '\~linux16 /vmlinuz-3\.10\.0-327\.el7\.x86_64~{s/$/ text1 text2/;}' /boot/grub2/grub.cfg 
+0

@RomanPerekhrest,非常感谢答复..我想在最后一行添加文本,而不是在下一行添加文本 –