In our work, we often need to replace and add URLs from the text using SED. However, we often encounter the ambiguity in the SED of special characters in the URL so that it takes a lot of time to test when writing a bash script. So what are the special characters that create ambiguity?
#这些都需要转义
&, |, \,/, ^, *, (,), [,], {,}, ',?
Which, to "&,/,?" " for the most important needs to transfer. For example:
[Email protected] test]$ url="HTTP://WWW.BAIDU.COM/CGI-BIN?A=DASDFD&B=ASDFASDF";Echo$url |sed "s/\ (\ (\/\) \|\ (\?\) \|\ (\&\) \)/\\\\\1/g"http:\/\/www.baidu.com\/cgi-bin\?a=dasdfd\&b=asdfasdf[[email protected] test]$ URL="HTTP://WWW.BAIDU.COM/CGI-BIN?A=DASDFD&B=ASDFASDF";Echo$url |sed "s/\ (\ (\/\) \|\ (\?\) \|\ (\&\) \)/\\\\\1/g"http:\/\/www.baidu.com\/cgi-bin\?a=dasdfd\&b=asdfasdf
Isn't it complicated? It's like writing the underlying code:). Replacement lookups are the best time to bash, but depending on the version of SED, your ability to work can be greatly diminished. Very disappointing. As above, the correct results will not be available on the sed-4.1.5 version. So, how important is fast python development! But even with a lot of problems, Bash still has a very important position, so it's best to get your bash programming level up and the version of each command at your fingertips. To be called a good engineer:)
Note: SED is not recognized (:?) Such Perl regular expressions, so do not use all (A|B), are escaped, such as \ (\), and \ (a\|b \).
*bash: How do I use Bash to escape special characters in the URL so that it doesn't have any ambiguity in sed?