0

I'm trying to script changes in a config file (/var/lib/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla), which boils down to:

[Update already installed software]
Identity=unix-group:admin;unix-group:sudo
Action=org.debian.apt.upgrade-packages
ResultActive=yes

[Printer administration]
Identity=unix-group:lpadmin;unix-group:admin;unix-group:sudo
Action=org.opensuse.cupspkhelper.mechanism.*
ResultActive=yes

[Disable hibernate by default in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=no

I'm trying to set ResultActive=yes for all blocks that start with [Disable hibernate. Using sed and regex groups I came up with:

sed -i 's/\(Disable hibernate.*\n.*\n.*\nResultActive\=\)no/\1yes/' /var/lib/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla

However this does not change the file. According to regexr, the regex matches, but checking with sed.js.org, sed doesn't change a thing.

How can I fix my sed command, to change the appropriate line for the hibernate config blocks?

edit: It seems like I cant get sed groups with newlines not to work at all.

1
  • 1
    sed reads line by line. This means there are no newlines in the pattern space to match. – Kusalananda Jan 27 '20 at 11:25
0

Sed is line-based by default - to do multi-line matches you would need to add lines to the pattern space explicitly (using the N command for example).

Instead you could do something like this which is still line-based but steps forward to load the line of interest:

$ sed '/^\[Disable hibernate/{n;n;n;/^ResultActive/s/=no/=yes/;}' file.pkla
[Update already installed software]
Identity=unix-group:admin;unix-group:sudo
Action=org.debian.apt.upgrade-packages
ResultActive=yes

[Printer administration]
Identity=unix-group:lpadmin;unix-group:admin;unix-group:sudo
Action=org.opensuse.cupspkhelper.mechanism.*
ResultActive=yes

[Disable hibernate by default in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes

Note that like your original, this will only work if the order of lines in the block can be relied on.

1
  • absolutely fantastic, thank you for the solution - sorry my reputation is too low to mark as useful. – GarlicCheese Jan 27 '20 at 12:14

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.