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.
Best Answer
A couple of mistakes there.
First, since
sed
uses basic regular expressions, you need\(
and\)
to make a capture group. The-r
switch enables extended regular expressions which is why you don't get the error. See Why do I need to escape regex characters in sed to be interpreted as regex characters?.Second, you are putting the capture group in the wrong place. If I understand you correctly, you can do this:
Note the use of
!
as regex delimiters to avoid having to escape the forward slashes in/dev/stdout
.