2

I'm writing a shell script that modifies GRUB options:

sed "/^GRUB_CMDLINE_LINUX_DEFAULT=/ s/$/ mem_sleep_default=deep /" /etc/default/grub

This sed command matches the line GRUB_CMDLINE_LINUX_DEFAULT="quietipv6.disable=1" but adds mem_sleep_default after the quoutes like this:

GRUB_CMDLINE_LINUX_DEFAULT="quietipv6.disable=1" mem_sleep_default=deep

How can I modify this sed to insert `mem_sleep_default inside the qoutes?

2

You're currently matching on the end-of-line with the search pattern s/$/.

Instead you might want to match on the trailing double quote followed by the EOL with s/\"$/ (Note that you need to escape the double quote with a backslash \")

In the substitution pattern you will need to return that trailing double quote to the end of the line:

sed "/^GRUB_CMDLINE_LINUX_DEFAULT=/ s/\"$/ mem_sleep_default=deep\"/" /etc/default/grub
3

try

sed "/^GRUB_CMDLINE_LINUX_DEFAULT=/ s/\(\"[^\"]*\)$/ mem_sleep_default=deep &/"

where

  • \(\"[^\"]*\)$/ tell sed to remember \( ... \) a quoted quote \" followed by any other char [^\"]* followed by end of line $
  • & will insert remembered pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.