0

I am trying to replace a string which is currently output as

http://domain1.com/subfolder1/http://domain2.com/subfolder2

so that it will only output as

http://domain2.com/subfolder2

I am using SED to replace a URL with the latter domain but I'm unable to get it to work. It works if I set "domain1" to be replaced by "domain2" however I want to be able to replace the whole string.

I'm trying to do this will the following code:

for FILE in `cat/WORKDIR/$inputControlFileName`; do sed -i -e  "s~$SEARCHTEXT~$REPLACEWITH~g" $OutputDirectory/$FILE; done;

My search parameter is http://domain1.com/subdomain/http://domain2.com/subdomain and replace parameter is http://domain2.com/subdomain

Appreciate any guidance.

Update Thanks you all for your help, I ended up getting this to work by only outputting domain1.com/domain.2.com and then escaping the values stored in the variables.

  • why don't we truncate the first domain? – msp9011 Aug 23 at 13:06
1

Try this,

echo "http://domain1.com/subfolder1/http://domain2.com/subfolder2" | sed 's/^http.*http:/http:/'
http://domain2.com/subfolder2
  • But what if the URL contains http as a subfolder name? – deimos Aug 23 at 13:16
  • good catch.... hope we can overcome by suffixing second http with colon – msp9011 Aug 23 at 13:18
  • 1
    Seems right. Something like | sed 's/^http:\/\/.*http\:\//http:\/\//g' would do the trick. I can also suggest awk: | awk -F'http://' '{print "http://" $NF}' – deimos Aug 23 at 13:22
0

If you never have : in the URI, you can do this only using bash builtins.

function last_addr(){
    IFS=: 
    string="$1"
    array=($string)
    echo -n http:; echo ${array[-1]}; unset IFS
}

last_addr 'http://domain1.com/subfolder1/http://domain2.com/subfolder2'
  • Thanks, your comment about never having : gave me the solution. – Samosa Aug 23 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.