Regex 查找Tcl命令args符号并替换为sed命令
我正在尝试使用sed替换文件中具有tcl命令分隔符“[]”的字符串 例如: 字符串[$HelloWorld]必须替换为$HelloWorld。请注意,它没有括号,要修改的文件是TCL文件。我将如何使用sed命令执行此操作 我试过这个:Regex 查找Tcl命令args符号并替换为sed命令,regex,sed,replace,Regex,Sed,Replace,我正在尝试使用sed替换文件中具有tcl命令分隔符“[]”的字符串 例如: 字符串[$HelloWorld]必须替换为$HelloWorld。请注意,它没有括号,要修改的文件是TCL文件。我将如何使用sed命令执行此操作 我试过这个: sed -i 's@[$HelloWorld]@$HelloWorld@g' <file_path> 您需要转义[and],因为它们在regexp中被解释为字符类,而不是文字方括号: $ sed 's/\[$HelloWorld\]/$HelloWo
sed -i 's@[$HelloWorld]@$HelloWorld@g' <file_path>
您需要转义[and],因为它们在regexp中被解释为字符类,而不是文字方括号:
$ sed 's/\[$HelloWorld\]/$HelloWorld/g' file
string $HelloWord
您可以在此处使用捕获组:
$ sed 's/\[\($HelloWorld\)\]/\1/g' file
string $HelloWord
如果要从文件中删除所有方括号,请使用sed的//[]]//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
您需要转义[and],因为它们在regexp中被解释为字符类,而不是文字方括号:
$ sed 's/\[$HelloWorld\]/$HelloWorld/g' file
string $HelloWord
您可以在此处使用捕获组:
$ sed 's/\[\($HelloWorld\)\]/\1/g' file
string $HelloWord
如果要从文件中删除所有方括号,请使用sed的//[]]//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