Lets say I want to replace the following string within a textfile:
document.write(unescape("%3Cscript src='" + \ + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
I came up with this sed command, but surprisingly nothing happens at all.
sed -i "s/document.write(unescape(\"%3Cscript src=\"\' + \\ + \"google-analytics.com\/ga.js\' type=\'text\/javascript\'%3E%3C\/script%3E\"开发者_StackOverflow社区));//g" myfile.html
Whats wrong here?
- Backslashes should be doubly escaped, since they are expanded by both
sed
and the shell. Use\\\\
(becomes\\
in the shell, then\
insed
) - You're matching on
src="'
, but your file containssrc='"
.
Use
sed -i \
"s|document.write(unescape(\"%3Cscript src='\" + \\\\ + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));||g" \
myfile.html
I've replaced /
with |
to make it more readable (no more need to escape /
).
精彩评论