0

I'm trying to replace the following string in a wordpress sql file:

http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png

to

https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png

I tried the following command which obviously didn't work

sed -i "s#'http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png'#'https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png'#g" database.sql

Someone please help to understand where I missed. Thank you very much.

paldig
  • 1
  • `sed -i 's|http:\\\\/\\\\/firstdomain.com\\\\/qwerty\\\\/wp-content\\\\/uploads\\\\/2018\\\\/07\\\\/section-shape.png|https:\\\\/\\\\/seconddomain.com\\\\/wp-content\\\\/uploads\\\\/2019\\\\/06\\\\/section-shape.png|' database.sql` – Cyrus Jun 30 '19 at 09:37
  • Thank you Cyrus, unfortunately this combination didn't change anything. – paldig Jun 30 '19 at 09:52
  • obviously the number of `\[\]` involved. Make a smaller test case, get that to work. I have seen cases where I needed 5 "\" chars, but it can be anything from 1 to 5, (maybe more!), so try them all. Good luck. – shellter Jun 30 '19 at 17:02
  • and ... `echo "http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png" | sed 's#http:\\/\\/firstdomain.com\\/qwe rty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png#https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape. png#'` produces the output you need. There must be something else, Are you trying to do this in `php`? Why not include a small sample of the actual file in your Q so we can test appropriately? Good luck. – shellter Jun 30 '19 at 17:59
  • @shelter off the top of my head, I think that if you ever need more than 2 backslashes (maybe even 1) then you've got your quoting wrong. `echo "http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png"`, for example, should be `echo 'http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png'` instead (i.e. single rather than double quotes) so the shell doesn't strip one of every pair of quotes. – Ed Morton Jun 30 '19 at 18:33

2 Answers2

1

You can't seriously apply a sed to a .db file because... well, it's database file not text (most likely sqlite by the way).

Instead, you should perform the string replacement with an (UPDATE) SQL query from the SQLite console (or whatever SQL client you have). Check this link for the replace method in SQLite for example.

Lucas
  • 1,833
  • 1
  • 18
  • 19
  • Thanks for your answer, but the file is a backup for mysql database and it is a text file. – paldig Jun 30 '19 at 09:30
  • Ok. You should have mentioned it then. Could you post parts of your DB backup file then, showing the looks of your paths to be replaced? – Lucas Jun 30 '19 at 09:32
  • I mentioned everything in my post. I just need to replace the string as how it looks with the new one. – paldig Jun 30 '19 at 10:04
0

Your first mistake is enclosing your script in double quotes instead of single, thereby inviting the shell to parse its contents before sed gets to see it and thus eating up one layer of backslashes.

If you have to deal with single quotes (which you shouldn't given your posted sample input but anyway...) never do this:

sed "s/foo'bar/stuff/"

do this instead:

sed 's/foo'\''bar/stuff/'

so the shell isn't interpreting every part of your script.

Beyond that - sed doesn't understand literal strings (see Is it possible to escape regex metacharacters reliably with sed), so instead just use a tool that does, e.g. awk:

awk '
  BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]="" }
  s=index($0,old) { $0 = substr($0,1,s-1) new substr($0,s+length(old)) }
1' \
  'http:\\/\\/firstdomain.com\\/qwerty\\/wp-content\\/uploads\\/2018\\/07\\/section-shape.png' \
  'https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png' \
  file
https:\\/\\/seconddomain.com\\/wp-content\\/uploads\\/2019\\/06\\/section-shape.png
Ed Morton
  • 188,023
  • 17
  • 78
  • 185