0

I have a problem when I'm trying to regex a line in a document with sed. Here is what I want to regex:

proxy_pass http:\\193.168.19.35/;

I want to obtain the following result:

proxy_pass http:\\193.168.19.32/;

This is what I have tried:

sed -r 's/^proxy_pass htpp\:\/\/[[:digit:]]+.*\/;/proxy_pass htpp\:\/\/192.168.81.29\/;/' /etc/nginx/nginx.conf > /etc/nginx/nginx.conf.temp

Can you guys help me here?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
André
  • 23
  • 4
  • 3
    misspelling? `htpp` -> `http`? – MuertoExcobito May 27 '15 at 17:32
  • 1
    No matter how many times you replace something with `192.168.81.29`, it will never end up with the result `193.168.19.32`... Making your question consistent would probably be a good idea... – twalberg May 27 '15 at 21:04
  • See http://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29626460#29626460 – Ed Morton May 28 '15 at 02:50

1 Answers1

2

If you have a URL with \\, you need to add support for this pattern in your regex. I believe you just misspelt some words in your question.

So, if you want to match proxy_pass http:\\193.168.19.32/;, use

sed -r 's/^proxy_pass http\:\\\\[[:digit:]]+(\.[[:digit:]]+)*\/;/proxy_pass htpp\:\/\/192.168.81.29\/;/' /etc/nginx/nginx.conf > /etc/nginx/nginx.conf.temp
                            ^^^^            |---------------|

If you want to replace proxy_pass http://193.168.19.32/;, use

sed -r 's/^proxy_pass http\:\/\/[[:digit:]]+(\.[[:digit:]]+)*\/;/proxy_pass htpp\:\/\/192.168.81.29\/;/' /etc/nginx/nginx.conf > /etc/nginx/nginx.conf.temp
                            ^  ^            |---------------|

Pay attention to (\.[[:digit:]]+)* in the pattern, it makes it possible to match all the 4 parts of the IP address.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563