Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sed to change conf file

Tags:

linux

sed

I'm trying to set some numeric values in my nginx.conf file. I'm calling sed from the RUN command in my Dockerfile.

Specifically I'm wanting to change what is currently:

worker_processes 4;

To:

worker_processes 1;

Regular expressions are powerful, but $*(&(*Y hard to make work.

I found this, which works.

 sed -i '/access_log/s|/[^;]\+|/dev/stdout|' /etc/nginx/nginx.conf

But modified to this and it doesn't.

 sed -i '/worker_processes/s|/[^;]\+|1|' /etc/nginx/nginx.conf

If someone could explain how the expression works the first time and not the second and how to make it work, that'd be great.

like image 235
hookenz Avatar asked Dec 05 '14 00:12

hookenz


People also ask

How do you replace a variable in a file using sed?

How SED Works. In the syntax, you only need to provide a suitable “new string” name that you want to be placed with the “old string”. Of course, the old string name needs to be entered as well. Then, provide the file name in the place of “file_name” from where the old string will be found and replaced.

Does sed command change the original file?

Note that sed doesn't alter the original file, so all changes will show in the output, but the original file remains the same for each command we successively run.


1 Answers

This worked for me in the end.

sed -i 's/^worker_processes.*/worker_processes 1;/' /etc/nginx/nginx.conf
like image 109
hookenz Avatar answered Sep 28 '22 05:09

hookenz