Skip to content
Advertisement

Shell script to edit and add a entry in file

Could any one help me to add entry at the end of the line.

This is linux os.

Below is the contents of the file /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
}

I need to add entry at the end of the line “text1” and “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

after edit my file should look like:

### 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
}

Advertisement

Answer

With sed expression:

sed '~linux16 /vmlinuz-3.10.0-327.el7.x86_64~{s/$/ text1 text2/;}' /boot/grub2/grub.cfg
Advertisement