I'm trying to edit my nginx.conf file programmatically, which contains a line like this:
access_log /var/log/nginx/access.log;
which I want to look like this:
access_log /dev/stdout;
I believe the regex ^\s*access_log([^;]*);
will capture the /var/log/nginx/access.log
part in a capture group, but I'm not sure how to correctly replace the capture group with sed?
I've tried
echo " access_log /var/log/nginx/access.log;" | sed 's/^\s*access_log([^;]*);/\1\\\/dev\\\/stdout/'
but I'm getting the error:
sed: -e expression #1, char 45: invalid reference \1 on `s' command's RHS
if I try sed -r
then there is no error, but the output is not what I expect:
/var/log/nginx/access.log\/dev\/stdout
I'm trying to be smart with the capture group and whatnot and not search directly for "access_log /var/log/nginx/access.log;" in case the distribution changes the default log file location.
/access_log/
string in Jeremy's is known as an "address" for thesed
command. Often, numbers (indicating line numbers) are used as addresses to sed but today I learned that regular expressions can be used as addresses too. Thanks Jeremy! – dgtc Mar 17 '21 at 16:58