标签:
我们在编写bash脚本的时候,经常需要替换掉字符窗中特殊的字符,我们看看有几种方法可以实现。
[nhuang@localhost test]$ a="This is a / and you will know /" ; echo "$a" | sed "s/\//\\\\\//g" This is a \/ and you will know \/
但是相当复杂,应为在替换部分要使用双反斜杠,"\\\\\/",而不是"\\\/",为什么呢?应为在escape"\“的时候,必须要使用"\\",而不是对待"/"的"\/"。例如
[nhuang@localhost test]$ a="This is a / and you will know /" ; echo "$a" | sed -s "s/\//\//g" This is a / and you will know / [nhuang@localhost test]$ a="This is a / and you will know /" ; echo "$a" | sed -s "s/\//\\\\/g" This is a \ and you will know [nhuang@localhost test]$ a="This is a / and you will know /" ; echo "$a" | sed -s "s/\//\\/g" sed: -e expression #1, char 8: unterminated `s‘ command
[nhuang@localhost test]$ a="This is a / and you will know /" ; echo ${a//\//\\} This is a \ and you will know [nhuang@localhost test]$ a="This is a / and you will know /" ; echo ${a//\//\\\/} This is a \/ and you will know \/
这里,我们不再需要使用"\\"了,而是正常的使用正则表达式。所以,第二种方法,最为方便。如果使用expr substr,则会把问题复杂化。dan
[nhuang@localhost test]$ str="asdasdf / asdfasdf / asdf,adf" ; echo $str | sed "{s/\//\\\\\//g;s/,/:/g}" asdasdf \/ asdfasdf \/ asdf:adf
资料:
http://www.cnblogs.com/frydsh/p/3261012.html
标签:
原文地址:http://www.cnblogs.com/nhuang2/p/5767840.html