Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 查找Tcl命令args符号并替换为sed命令_Regex_Sed_Replace - Fatal编程技术网

Regex 查找Tcl命令args符号并替换为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替换文件中具有tcl命令分隔符“[]”的字符串

例如:

字符串[$HelloWorld]必须替换为$HelloWorld。请注意,它没有括号,要修改的文件是TCL文件。我将如何使用sed命令执行此操作

我试过这个:

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