格式的给定输入
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<#>更新:
一开始我没说清楚。“肮脏2”S什么时候会出现?_平台_搜索_提示可能是“否”的问题有更多信息。也就是说,还有其他的if语句,我只想处理feature_platform_search_hint语句。现在把另一个if案例放在上面。
我希望我的sed在search条件下选择第一个feature_platform_search_hint命令,而忽略/删除整个if命令块:
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其余的/剩余的线路完好无损。
下面是我提出的sed命令:
/feature_platform_search_hint/{
# remove if line and keep next
d; N; h;
# remove else block
N; N; N; d;
g; s/ search /search /;
}但它并没有像我预期的那样起作用。为什么以及如何修复?thx
发布于 2021-10-20 07:34:53
sed '/^ *if \+/d;/^ *else *$/,/^ *fi *$/d' remove_if_block/^ *if在你的行上面,你有空格,所以空格和asterix前面的'if‘。我不知道你的文件是否真的有。但是,即使有这些代码,上面的代码也应该能工作。如果有人无意中在“after”和“fi”之后添加了空格,则else *$/和fi *$的空格用于保护.也可以从其他地方开始,所以.
发布于 2021-10-20 07:58:43
只需将if语句替换为if true; then,就会创建具有相同效果的shell代码。
sed 's/^ *if \[.*]; then/if true; then # &/' file这将替换if语句,但将原始代码保留在注释中。
发布于 2021-10-21 00:34:44
找到了我的解决方案--无法使用d,它将立即启动下一个周期,而其余的命令则没有处理。
# 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
9https://unix.stackexchange.com/questions/673938
复制相似问题