0

Given inputs in the format of

        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

. . .

if test x$grub_platform = xpc; then linux_suffix=16; else linux_suffix= ; fi

UPDATE:

I didn't make it clear at first. The question When will grub2's feature_platform_search_hint might be "No" has more info. I.e., there are other if statements, and I want only to process the feature_platform_search_hint one. Putting another if case above now.

I want my sed to pick the first search command under the feature_platform_search_hint condition while ignore/delete the whole if command block:

        set root='hd0,gpt3'
        search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt3 --hint-efi=hd0,gpt3 --hint-baremetal=ahci0,gpt3  dcf03c24-3d0d-4581-be1d-67b90f92a2c1
        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

The rest/remaining lines are intact.

Here is the sed command that I come up with:

/feature_platform_search_hint/{
# remove if line and keep next
d; N; h;
# remove else block
N; N; N; d;
g; s/  search /search /;
}

But it is not working as I expected.
Why and how to fix? thx

xpt
  • 1,530

3 Answers3

2
sed '/^ *if \+/d;/^ *else *$/,/^ *fi *$/d' remove_if_block

/^ *if
in you lines above you have white spaces so white space and asterix before 'if'.
I don't know if your file has them actually. But even if it has them this code above should work.
else *$/ and fi *$
white space after 'else' and 'fi' for protection if someone unintentionally added space after them... Also line could start with, say elsewhere, so...

Damir
  • 501
  • Thanks but the question is on multi-line pick & delete, using sed h, H and g or x operation. I'll wait for more answers or choose yours if I get no more. – xpt Oct 20 '21 at 12:21
  • @xpt, maybe I don't understand your requirement well. By multi-line pick & delete do you mean "sed, please set address to say, line2, line4, line5 and line6, and when you set that address, delete it (those lines, 2,4,5 and 6) with 'd' at once? Are you trying to set arbitrary 'multi-non-consecutive-lines' address? Maybe you are trying to 'set up' address containing lines at your will, say address 7, 10, 15, 20to24 and 55? If that is your requirement, as I know sed cannot do that without using '-e' or ';'. – Damir Oct 20 '21 at 13:12
  • @xpt, sorry, I just noticed, that I completely missed your part about 'hold space'... – Damir Oct 20 '21 at 13:23
  • @xpt maybe I'm wrong, but in your question there is no requirement that 'holding space' must be used. Are you trying to achieve this sed '2d; 4,6d? I'm just curious :) - why do you want/must use 'holding' sed feature? – Damir Oct 20 '21 at 14:17
  • My bad actually, I didn't make it clear. The question https://unix.stackexchange.com/questions/673646/ has more info. I.e., there are other if statements, and I want only to process the feature_platform_search_hint one. Your solution works perfectly for the sample in this case, just not what I was thinking, which I didn't make clear in the first place. accepting yours and I'll give my solution below. – xpt Oct 21 '21 at 00:30
1

Simply replacing the if statement with if true; then would create shell code with the same effect.

sed 's/^ *if \[.*]; then/if true; then # &/' file

This replaces the if statement, but leaves the original code intact in a comment.

Kusalananda
  • 333,661
  • Wow! Very interesting approach. For someone who will may want 'else' part to be executing he/she can just change 'if true' to 'if false'. – Damir Oct 20 '21 at 08:34
  • 1
    @Damir ... or make sure that the variable feature_platform_search_hint is not set to y in the original code. – Kusalananda Oct 20 '21 at 08:49
  • Thanks but that's not the output I want, listed clearly in OP. The question is on multi-line pick & delete, using sed h and g operation. – xpt Oct 20 '21 at 12:18
  • @xpt Sure, it's not exactly the output you want. It's just an equivalent shell script. – Kusalananda Oct 20 '21 at 12:27
  • for sure @ they. Take a look at https://unix.stackexchange.com/questions/673646/, for why I'm doing this. – xpt Oct 20 '21 at 12:29
  • @xpt I'm not certain I understand what issue you are solving by removing the if statement though. Especially not if you are uncertain of whether it may ever be selecting the false branch or not. This seems like a solution in search for a problem. – Kusalananda Oct 20 '21 at 13:14
  • @they, I'm not sure what your understanding is from the above https://unix.stackexchange.com/questions/673646/. To me, the last line, "I repeat, "the conditional code checking for this feature is pointless in all modern Linux distributions"" summarizes the whole situation quite well, which makes this question a logic next step. – xpt Oct 20 '21 at 14:44
  • @xpt "Pointless" is one thing, and "needs fixing" is another. Presumably whoever maintains the current implementation does not think it needs modifying, or knows of reasons why it shouldn't be modified. I suggest that you submit a patch to the correct location to have it fixed upstream rather than trying to come up with a solution that possibly works in the cases that you are aware of. – Kusalananda Oct 20 '21 at 15:06
  • I assume you are more capable doing all what you suggested than me, using the same assumption of what you had on me, since you are so keen on it and don't mind spending all the time spelling all those out. Go ahead and do it yourself, instead of asking others to do it. Good luck. – xpt Oct 20 '21 at 16:32
  • @xpt Sorry, but it was your issue. I don't even use grub... – Kusalananda Oct 20 '21 at 16:42
  • grub is used by every single linux user, how come it becomes my issue? – xpt Oct 20 '21 at 16:44
  • @xpt I don't run Linux. If you think every Linux user would benefit from the modification, then it sure sounds to me that it's up to you to push for a patch since nobody else seems to care about whether this needs fixing or not. If you need something changed, you can't sit around waiting for somebody else to provide the work. Who knows, it might just be enough with one polite email to a developer to point out the pointlessness of that if statement. – Kusalananda Oct 20 '21 at 16:50
0

Found my solution -- cannot use d, which will start the next cycle immediately, leaving the rest of commands not processed.

# input:
$ seq 9
1
2
3
4
5
6
7
8
9

goal: remove line 4 (if), and 6~8 (3-line else block)

$ seq 9 | sed '/4/{N; s/^.*\n//; h; N; N; N; g; }' 1 2 3 5 9

xpt
  • 1,530