You're viewing a comment by Artym and its responses.

Artym Permalink
July 03, 2016, 16:46

Hi there!
Could anyone explain to me how to work this code?

before:
access_log /var/log/nginx/access.log main;

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

after:
access_log /dev/stdout;

or tell me where I can see an explanation.
I'm beginner in sed, and I've never faced the code like this.
Thank you in advance.
Please excuse my poor English.

Comment Responses

July 03, 2016, 18:42

There are three parts to your code:

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

First, -i argument that replaces files Inplace, meaning it replaces file content without creating extra files.

Second, /access_log/ code part that makes sed only match lines that contain string "access_log", and apply the command that follows it to these lines.

Third, s|/[^;]\+|/dev/stdout| command.

This command only gets applied to linse that match "access_log".

It's the "s" command meaning substitute (replace).

There are two parts to this substitute command s|FIND|REPLACE| - FIND and REPLACE.

In your code FIND is /[^;]\+ and REPLACE is /dev/stdout.

Your find part is a regular expression that says "start matching at / character and match as many characters as possible [^;]\+ until you hit the ; character."

Your replace part is simply string "/dev/stdout".

Given your input:

access_log /var/log/nginx/access.log main;

The find part in your substitute command matches first / then keeps matching until it hits the ending ; and then replaces it all with /dev/stdout.

Artym Permalink
July 06, 2016, 05:15

Thank you very much, Peter!

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "sandbox_95": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.