2013-02-01 89 views

回答

4

你需要逃避[],因爲它們解釋爲正則表達式字符類,而不是字面方括號:

$ sed 's/\[$HelloWorld\]/$HelloWorld/g' file 
string $HelloWord 

您可以使用捕捉組:

$ sed 's/\[\($HelloWorld\)\]/\1/g' file 
string $HelloWord 

使用sed 's/[][]//g'如果要從文件中刪除所有方括號:

# First check changes are correct 
$ sed 's/[][]//g' file 
string $HelloWorld 

# Store the change back to the file 
$ sed -i 's/[][]//g' file 

# Store changes back to the file and create back up of the original 
$ sed -i.bak 's/[][]//g' file