附加到文件中的现有变量

ota*_*ike 4 sed awk text-processing

我有一个 Grub 选项文件,我想通过脚本进行修改。我想添加额外的引导加载程序选项。

该文件可能会随着时间的推移而改变,所以我想通过附加到图像附带的任何引导加载程序选项来创建适应未来版本的内容。

例如,引导加载程序文件包含以下内容:

# Set the recordfail timeout
GRUB_RECORDFAIL_TIMEOUT=0

# Do not wait on grub prompt
GRUB_TIMEOUT=0

# Set the default commandline
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0"

# Set the grub console type
GRUB_TERMINAL=console
GRUB_HIDDEN_TIMEOUT=0.1
Run Code Online (Sandbox Code Playgroud)

我想附加到行 GRUB_CMDLINE_LINUX_DEFAULT 以便它最终像:

GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0 cgroup_enable=memory swapaccount=1"
Run Code Online (Sandbox Code Playgroud)

有没有办法用 sed 或 awk 做到这一点?类似于“在包含 GRUB_CMDLINE_LINUX_DEFAULT 的行的最后一个引号之前附加此内容。”

提前致谢!

Rom*_*est 5

只需sed

sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="[^"]*/& cgroup_enable=memory swapaccount=1/' file
Run Code Online (Sandbox Code Playgroud)
  • [^"]* - 匹配除双引号外的任何字符 "
  • & - 整个匹配的字符串(模式空间)