Sed's substitution command typically uses /
as the delimiter between the s
command, the search string, the replacement string, and any flags. If, however, either of the search string or replacement string may contain a /
, then people will change the delimiter to a character that is not in either of the strings.
You received the other error from sed when you told it to use an empty string as the backup extension; since you're not using an extension, don't put the empty quotes in. (Note: This only applies to GNU sed. If you're using BSD sed, e.g. on a Mac, then you do need the empty string to specify no backup extension.)
Sed does not care what the delimiter is, but your surrounding shell might.
The |
symbol introduces a pipe. To use it as a delimiter, you would have to quote it in some fashion to protect them from the shell.
The #
symbol may introduce a comment string, based on your shell (bash only begins a comment if the #
is the first character of a word), so better to be safe and quote it.
Since your data contains /
characters, you either need to use a different delimiter, or escape every instance of /
in the search and replacement strings. This leads to what's known as leaning toothpick syndrome because of the hard-to-read appearance of the \/
.
Thus, you'd need something like:
find . -type f -name 'file.sql' -exec sed -i -e 's#http://a.com#http://b.com#g' {} +
or
find . -type f -name 'file.sql' -exec sed -i -e 's|http://a.com|http://b.com|g' {} +
or
find . -type f -name 'file.sql' -exec sed -i -e "s|http://a.com|http://b.com|g" {} +
or
find . -type f -name 'file.sql' -exec sed -i -e 's/http:\/\/a.com/http:\/\/b.com/g' {} +
s...g
, then only the second is a syntax error. Neither problem has anything to do withfind
. – Satō Katsura Jun 04 '17 at 15:04's|'"http://a.com"'|'"http://b.com"'|g'
any of the line above will work, but the question still remains open, what are the difference between#
,%
,/
and|
? – Stickers Jun 04 '17 at 16:10%
version later, didn't it work? – ilkkachu Jun 04 '17 at 17:18s|...|g
etc. in single quotes. You don't need double quotes for the URLs. (2)s/.../g
has the leaning toothpick syndrome, because the delimiter/
fors
is also found inhttp://
and in the paths separators. (3) There is no difference in using#
,%
, or|
as delimiters forsed
, they're just alternative solutions to the leaning toothpick problem. Any other "spare" character would do. (4)man sed
,man bash
. – Satō Katsura Jun 04 '17 at 20:59#
and%
worked without any issues. – Stickers Jun 04 '17 at 21:37