Home > Enterprise >  This sed end-case doesn't make sense, and I don't know why
This sed end-case doesn't make sense, and I don't know why

Time:10-21

I found the following sed end-case doesn't make sense, but I don't know why, and I cannot simplify it any further.

Basically I want to change one line and delete a range of lines, like this:

$ seq 9 | sed '/3/s/$/==/; /4/,/6/d;'
1
2
3==
7
8
9

However, when it comes to the following actual code, that I want to change one line and delete a range of lines, it doesn't work for me any more (if I uncomment the first command):

$ cat ./grub2clean.why-0.sed
#/menuentry/{ s/^/\n/; s/--\(class\|id\) .*$/\{/ };

# WHY?!
/Ubuntu/s/Ubuntu/UUUU/;
/^submenu /,/^}/d;
/UUUU/s/UUUU/!Ubuntu/;

I am running:

sed -f ./grub2clean.why-0.sed grub.test.cfg

if I un-comment the first command, the delete-a-range-of-lines command, which is in the middle of two working substitutions, no longer works any more.

This is so~~~~ strange and I don't know why.

Please, somebody help PLEASE!

The grub.test.cfg file is:

more here
menuentry 'Ubuntu 20.04 LTS (20.04) (on /dev/sda3)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-simple-dcf03c24-3d0d-4581-be1d-67b90f92a2c1' {
        insmod part_gpt
        insmod ext2
        set root='hd0,gpt3'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt3 --hint-efi=hd0,gpt3 --hint-baremetal=ahci0,gpt3  dcf03c24-3d0d-4581-be1d-67b90f92a2c1
        else
          search --no-floppy --fs-uuid --set=root dcf03c24-3d0d-4581-be1d-67b90f92a2c1
        fi
        linux /boot/vmlinuz-5.4.0-33-generic root=UUID=dcf03c24-3d0d-4581-be1d-67b90f92a2c1 ro net.ifnames=0
        initrd /boot/initrd.img-5.4.0-33-generic
}
submenu 'Advanced options for Ubuntu 20.04 LTS (20.04) (on /dev/sda3)' $menuentry_id_option 'osprober-gnulinux-advanced-dcf03c24-3d0d-4581-be1d-67b90f92a2c1' {
        menuentry 'Ubuntu (on /dev/sda3)' --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-/boot/vmlinuz-5.4.0-33-generic--dcf03c24-3d0d-4581-be1d-67b90f92a2c1' {
                insmod part_gpt
                insmod ext2
                set root='hd0,gpt3'
                if [ x$feature_platform_search_hint = xy ]; then
                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt3 --hint-efi=hd0,gpt3 --hint-baremetal=ahci0,gpt3  dcf03c24-3d0d-4581-be1d-67b90f92a2c1
                else
                  search --no-floppy --fs-uuid --set=root dcf03c24-3d0d-4581-be1d-67b90f92a2c1
                fi
                linux /boot/vmlinuz-5.4.0-33-generic root=UUID=dcf03c24-3d0d-4581-be1d-67b90f92a2c1 ro net.ifnames=0
                initrd /boot/initrd.img-5.4.0-33-generic
        }
        menuentry 'Ubuntu, with Linux 5.4.0-33-generic (recovery mode) (on /dev/sda3)' --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-/boot/vmlinuz-5.4.0-33-generic-root=UUID=dcf03c24-3d0d-4581-be1d-67b90f92a2c1 ro recovery nomodeset-dcf03c24-3d0d-4581-be1d-67b90f92a2c1' {
                insmod part_gpt
                insmod ext2
                set root='hd0,gpt3'
                if [ x$feature_platform_search_hint = xy ]; then
                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt3 --hint-efi=hd0,gpt3 --hint-baremetal=ahci0,gpt3  dcf03c24-3d0d-4581-be1d-67b90f92a2c1
                else
                  search --no-floppy --fs-uuid --set=root dcf03c24-3d0d-4581-be1d-67b90f92a2c1
                fi
                linux /boot/vmlinuz-5.4.0-33-generic root=UUID=dcf03c24-3d0d-4581-be1d-67b90f92a2c1 ro recovery nomodeset
                initrd /boot/initrd.img-5.4.0-33-generic
        }
}

menuentry 'Ubuntu 19.04 (19.04) (on /dev/sda7)' --class ubuntu --class gnu-linux -
-class gnu --class os $menuentry_id_option 'osprober-gnulinux-simple-db9e32bf-a209
-453f-9eb9-82123fc46652' {
        insmod part_gpt
        insmod ext2
        set root='hd0,gpt7'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt7 --hint-efi=
hd0,gpt7 --hint-baremetal=ahci0,gpt7  db9e32bf-a209-453f-9eb9-82123fc46652
        else
          search --no-floppy --fs-uuid --set=root db9e32bf-a209-453f-9eb9-82123fc46652
        fi
        linux /vmlinuz root=/dev/sda7
        initrd /initrd.img
}
submenu 'Advanced options for Ubuntu 19.04 (19.04) (on /dev/sda7)' $menuentry_id_option 'osprober-gnulinux-advanced-db9e32bf-a209-453f-9eb9-82123fc46652' {
        menuentry 'Ubuntu 19.04 (19.04) (on /dev/sda7)' --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-/vmlinuz--db9e32bf-a209-453f-9eb9-82123fc46652' {
                insmod part_gpt
                insmod ext2
                set root='hd0,gpt7'
                if [ x$feature_platform_search_hint = xy ]; then
                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt7 --hint-efi=hd0,gpt7 --hint-baremetal=ahci0,gpt7  db9e32bf-a209-453f-9eb9-82123fc46652
                else
                  search --no-floppy --fs-uuid --set=root db9e32bf-a209-453f-9eb9-82123fc46652
                fi
                linux /vmlinuz root=/dev/sda7
                initrd /initrd.img
        }
        menuentry 'Ubuntu 19.04 (19.04) (on /dev/sda7)' --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-/vmlinuz--db9e32bf-a209-453f-9eb9-82123fc46652' {
                insmod part_gpt
                insmod ext2
                set root='hd0,gpt7'
                if [ x$feature_platform_search_hint = xy ]; then
                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt7 --hint-efi=hd0,gpt7 --hint-baremetal=ahci0,gpt7  db9e32bf-a209-453f-9eb9-82123fc46652
                else
                  search --no-floppy --fs-uuid --set=root db9e32bf-a209-453f-9eb9-82123fc46652
                fi
                linux /vmlinuz root=/dev/sda7
                initrd /initrd.img
        }
        menuentry 'Ubuntu 19.04 (19.04) (on /dev/sda7)' --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-/vmlinuz--db9e32bf-a209-453f-9eb9-82123fc46652' {
                insmod part_gpt
                insmod ext2
                set root='hd0,gpt7'
                if [ x$feature_platform_search_hint = xy ]; then
                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt7 --hint-efi=hd0,gpt7 --hint-baremetal=ahci0,gpt7  db9e32bf-a209-453f-9eb9-82123fc46652
                else
                  search --no-floppy --fs-uuid --set=root db9e32bf-a209-453f-9eb9-82123fc46652
                fi
                linux /vmlinuz root=/dev/sda7
                initrd /initrd.img.old
        }
        menuentry 'Ubuntu 19.04 (19.04) (on /dev/sda7)' --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-/vmlinuz.old--db9e32bf-a209-453f-9eb9-82123fc46652' {
                insmod part_gpt
                insmod ext2
                set root='hd0,gpt7'
                if [ x$feature_platform_search_hint = xy ]; then
                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt7 --hint-efi=hd0,gpt7 --hint-baremetal=ahci0,gpt7  db9e32bf-a209-453f-9eb9-82123fc46652
                else
                  search --no-floppy --fs-uuid --set=root db9e32bf-a209-453f-9eb9-82123fc46652
                fi
                linux /vmlinuz.old root=/dev/sda7
                initrd /initrd.img.old
        }
}

more here

Tested under both of the following two OSs,

$ lsb_release -a 
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux bullseye/sid
Release:        10.10
Codename:       buster

and

$ lsb_release -a 
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:        20.04
Codename:       focal

$ sed --version
sed (GNU sed) 4.7
...

$ apt-cache policy sed
sed:
  Installed: 4.7-1
  Candidate: 4.7-1
  Version table:
 *** 4.7-1 500
        500 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages
        100 /var/lib/dpkg/status

CodePudding user response:

In your first sed command, you look for any line containing menuentry and stick a newline in as the first character of that line.

Then in the delete command, the range varies from a line starting with submenu to one starting with a closing brace.

When the script puts a newline in the first character of any line containing menuentry it also puts one as the first character of the lines starting with submenu because those lines also contain menuentry in the $menuentry_id_option bit.

So after the first sed command, there aren't any lines starting with submenu - all those lines start with a newline now.

So either your first sed command should match only lines that start with menuentry with optional leading whitespace, or the delete command should look for lines that start with a newline followed by submenu.

  • Related