*bash: How do I use Bash to escape special characters in the URL so that it doesn't have any ambiguity in sed?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.