-2

In my nginx configuration file I have the following lines:

location / {
        try_files $uri $uri/ =404;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

Now, I would like to comment out/uncomment the two lines programmatically:

        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;

So I thought about sed and found this post here: https://stackoverflow.com/a/27355109

Unfortunately I could not get it work. Perhaps there're problems with escapting the chars. Could someone help?

Aliquis
  • 2,091
  • 4
  • 21
  • 40
  • 2
    *"... I could not get it work ..."* is not a good problem statement. It lacks both the code you are using and the error you are encountering. – jww Jan 20 '19 at 20:20
  • do you want to comment them out based on their content on based on their location relative to the surrounding lines or something else? – Ed Morton Jan 21 '19 at 18:11

1 Answers1

2

To comment these lines out, use command

sed \
    -e 's|auth_basic "Restricted Content";|# \0|' \
    -e 's|auth_basic_user_file /etc/nginx/.htpasswd;|# \0|' \
    your_file

To uncomment, use this:

sed \
    -e 's|# \(auth_basic "Restricted Content";\)|\1|' \
    -e 's|# \(auth_basic_user_file /etc/nginx/.htpasswd;\)|\1|' \
    your_file
Oleg Andriyanov
  • 5,069
  • 1
  • 22
  • 36