-1

I need to replace char "\" and it is not working. my script as follow. I'm using same script for many other replacements and somehow this is not working.

export VCA='http:\\/\\/localhost\\/dev1'
export VCB='https:\\/\\/www.mydomain.com'
sed "s~${VCA}~${VCB}~g" tmp2.sql > tmp3.sql

I don't see error anymore but it does not replace the string.

jww
  • 97,681
  • 90
  • 411
  • 885
Roni
  • 1
  • 1
  • Try to double or even quadruple the backslashes or, if these are meant to be schema indicators/paths as used in URLs, change them to what is used there, forwardslashes. – sticky bit Jan 21 '19 at 01:09
  • Possible duplicate of [How to pass a variable containing slashes to sed](https://stackoverflow.com/q/27787536/608639). Also see [What characters do I need to escape when using sed in a sh script?](https://unix.stackexchange.com/q/32907/56041) on [Unix & Linux Stack Exchange](http://unix.stackexchange.com/). – jww Jan 21 '19 at 01:10
  • You only need to replace the HTTP->HTTPS right? If so, leave the slashes out of it. – Kingsley Jan 21 '19 at 01:37
  • The problem is that I have few HTTP that needs to stay as-is, only is specific cases it needs to be replaced. – Roni Jan 21 '19 at 02:28
  • 2
    Please have the question include a few relevant lines of `tmp2.sql`. – agc Jan 21 '19 at 05:51
  • 1
    Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jan 21 '19 at 06:36

1 Answers1

0

Try:

VCA='http://localhost/dev1' 
VCB='https://www.example.com'
sed "s~${VCA}~${VCB}~g" tmp2.sql > tmp3.sql

Note: stackoverflow's answer text editor seems to dislike having www.mydomain.com in the code, so $VCB uses a dummy value which can be changed back as needed.

agc
  • 7,973
  • 2
  • 29
  • 50
  • It worked! but didn't work when added full text needs to be replaced: http:\\/\\/localhost\\/dev1 to https:\\\/\\\/www.mydomain.com – Roni Jan 21 '19 at 02:25
  • @Roni, Please quote the actual code with the "full text" in question. – agc Jan 21 '19 at 02:36
  • @Roni Your regex requires there to be a literal backslash before each slash. Slashes are not special characters per se; if, like you already do, you use `~` as the separator, the slashes do not need any special treatment. – tripleee Jan 21 '19 at 07:16
  • As another aside, the `export` statements seem superfluous in this context (though it is of course possible that you are using these variables for something else as well where they do need to be visible to subprocesses). – tripleee Jan 21 '19 at 07:16
  • Bottom line, I just need to replace the 2 strings in a script, don't have to use variable - any suggestions? – Roni Jan 21 '19 at 15:28
  • 1
    @Roni bottmi line is that sed doesn't understand strings, it looks for a regexp and replaces the matching text with backreference-enabled text. It's extremely difficult to get sed to treat it's regexp and replacement text as if they were literal strings (see https://stackoverflow.com/q/29613304/1745001) so if you want to replace a literal string with another literal string then you should just use a tool that understands strings, e.g. awk. If you add sample input/output to your question as requested in the comments under it then we can help you. – Ed Morton Jan 21 '19 at 18:02