0

Trying to write a one liner to change my wallpaper through a config file. Tried adapting from: Replace regex capture group content using sed but I can't seem to get it right.

My config file contains the following string:

output * bg ~/pictures/wallpapers/old-wallpaper.jpg fill

I want to change the path in this string to something else.

This one makes most sense to me but doesn't capture correctly:

sed "/output \* bg /s|.* fill|~/path/to/foobar.jpg fill|" sed_file

output * bg ~/pictures/wallpapers/old-wallpaper.jpg fill

This one looks completely wrong but seems to get the job done. Although I think it replaces anything that starts with output.

sed "/output \* bg/s| .* fill| \* bg ~\/path\/to/foobar.jpg fill|" sed_file

output * bg ~/path/to/foobar.jpg fill

mork
  • 103
  • 2

1 Answers1

1

With GNU sed. I assume your path contains no spaces or pipes. I switched from s/// to s|||.

sed -E 's|^(output \* bg) [^ ]+ |\1 foo |' file

Output:

output * bg foo fill

-E: use extended regular expressions

Cyrus
  • 12,309