0

file content

something
OIDCRedirectURI http://abc-mt.tc.ac.com/newredirect
something

sed command tried

URL='xyz-new.com' #This will be forming at run time

sed -i 'abc /c\ OIDCRedirectURI $URL/newredirect' /etc/httpd/conf.d/proxy.conf

Basically I want to replace the given URL with the new URL.

But is it replacing with $URL.

Any pointers?

5
  • Use double instead of single quotes. Also, the "assignment" $URL = '...' is actually a command. Write url='...' instead. I recommend shellcheck.net. Commented May 20, 2020 at 7:46
  • Your sed substitution syntax is not correct. Pls show a line as an example from your file, before and after. Commented May 20, 2020 at 7:51
  • @Kent I update the question so you have more clarity now. Commented May 20, 2020 at 8:00
  • @Socowi Double quotes not working in my case. I update the question so you have more clarity. Commented May 20, 2020 at 8:01
  • In the updated question you are still using single quotes ' instead of double quotes ". Apart from that, your main problem is the sed command as pointed out by Kent. Fixing the quotes is only the first step. Commented May 20, 2020 at 8:04

2 Answers 2

1

Assume the new URL is saved in a shell variable: $URL This one-liner may help you:

sed -i "s@\(^OIDCRedirectURI \).*@\1$URL/newredirect@" file

In your example, the URL is not with a protocol, e.g. HTTP or https. If you want to "reuse" the protocol prefix from the "old" URL, you can add it to the capture group:

sed -i "s@\(^OIDCRedirectURI http[^/]*//\).*@\1$URL/newredirect@" file

just to show it is working:

kent$  cat /tmp/test/f
something
OIDCRedirectURI http://abc-mt.tc.ac.com/newredirect
something

kent$  URL='this.is.new.url'

kent$  sed -i "s@\(^OIDCRedirectURI http[^/]*//\).*@\1$URL/newredirect@" /tmp/test/f

kent$  cat /tmp/test/f
something
OIDCRedirectURI http://this.is.new.url/newredirect
something
Sign up to request clarification or add additional context in comments.

4 Comments

It's giving me an error: No such file or directory. conf.d/proxy.conf, This is because I need to pass the full path of file - /etc/httpd/conf.d/proxy.conf. and the file path / is getting mixed with sed command /.
@DeveshAgrawal No it won't. the separator of s in sed is @, the / is not a problem. It must be something else.
@DeveshAgrawal check the test in answer
Thanks a lot! Finally, it seems working. There was definitely some issue at my end.
0

I wouldn't use sed for this, personally.

#!/bin/sh -x

mystring="OIDCRedirectURI http://abc-mt.tc.ac.com/newredirect"
redir=$(echo -e "${mystring}" | cut -d' ' -f1)
oldurl=$(echo -e "${mystring}" | cut -d' ' -f2)
newurl="http://xyz-new.com"

echo -e "${redir} ${newurl}"

Granted, you probably want to do this in a loop with a list of such entries, but that wouldn't be too difficult. You'd just have to put your old URLs and new URLs in two stack files, and make sure that they were in the correct sequence within each.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.