sed は、特定の行の後にテキストを配置します。

sed は、特定の行の後にテキストを配置します。

Sedのマンページを読んでいますが、まだ混乱しているので、ここで答えを見つけることができることを願っています。 :)

Bashスクリプトを使用してファイルを編集する必要があります。

grub.cfgの特定の行の後に数行を配置する必要があります。例を示すために5つの新しい行を追加します。

オリジナル:

if loadfont /boot/grub/font.pf2 ; then
    set gfxmode=auto
    insmod efi_gop
    insmod efi_uga
    insmod gfxterm
    terminal_output gfxterm
fi

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

menuentry "Install Ubuntu Server" {
    set gfxpayload=keep
    linux   /install/vmlinuz  file=/cdrom/preseed/ubuntu-server.seed quiet ---
    initrd  /install/initrd.gz
}
menuentry "OEM install (for manufacturers)" {
    set gfxpayload=keep
    linux   /install/vmlinuz  file=/cdrom/preseed/ubuntu-server.seed quiet oem-config/enable=true ---
    initrd  /install/initrd.gz
}

編集後:

if loadfont /boot/grub/font.pf2 ; then
    set gfxmode=auto
    insmod efi_gop
    insmod efi_uga
    insmod gfxterm
    terminal_output gfxterm
fi

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
newly_addedd_line_1
newly_addedd_line_2
newly_addedd_line_3
newly_addedd_line_4
newly_addedd_line_5

menuentry "Install Ubuntu Server" {
    set gfxpayload=keep
    linux   /install/vmlinuz  file=/cdrom/preseed/ubuntu-server.seed quiet ---
    initrd  /install/initrd.gz
}
menuentry "OEM install (for manufacturers)" {
    set gfxpayload=keep
    linux   /install/vmlinuz  file=/cdrom/preseed/ubuntu-server.seed quiet oem-config/enable=true ---
    initrd  /install/initrd.gz
}

sedを使用するとどのように機能しますか?理想的には、すべての新しい行を新しいスクリプト変数に入れることです。

たとえば、

LINES_TO_ADD='newly_addedd_line_1\n
newly_addedd_line_2\n
newly_addedd_line_3\n
newly_addedd_line_4\n
newly_addedd_line_5'

よろしくお願いします!

ベストアンサー1

sed '/^set menu_color_highlight/a\
newly_addedd_line_2\
newly_addedd_line_3\
newly_addedd_line_4\
newly_addedd_line_5' /path/to/file

出力を確認したら、sed --in-place新しいファイルを使用または作成し、古いバージョンを置き換えることができます。

マニュアルから:

 [1addr]a\
 text    Write text to standard output immediately before each attempt to read
         a line of input, whether by executing the ``N'' function or by beginning
         a new cycle.

おそらく少し強力です。新しいテキストブロックをファイル(たとえばnewlines)に入れてから、次のものを使用できます。

sed '^set menu_color_highlight/r newlines' /path/to/input.

また、マニュアルで:

 [1addr]r file
         Copy the contents of file to the standard output immediately before the
         next attempt to read a line of input.  If file cannot be read for any
         reason, it is silently ignored and no error condition is set.

おすすめ記事