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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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 如何替换包含方括号的字符串_Regex_Bash_Sed - Fatal编程技术网

Regex 如何替换包含方括号的字符串

Regex 如何替换包含方括号的字符串,regex,bash,sed,Regex,Bash,Sed,请帮帮我。 存在具有以下字符串的文件: <b>[source:1:2:3]</b> <b>[source:2:3:1]</b> <b>[source:3:1:2]</b> <b>[source:1:3:2]</b> [来源:1:2:3] [来源:2:3:1] [来源:3:1:2] [来源:1:3:2] 我只需要从这个文件中转换一行。结果应该是这样一个文件: <b>[source:1:2

请帮帮我。 存在具有以下字符串的文件:

<b>[source:1:2:3]</b>
<b>[source:2:3:1]</b>
<b>[source:3:1:2]</b>
<b>[source:1:3:2]</b>
[来源:1:2:3]
[来源:2:3:1]
[来源:3:1:2]
[来源:1:3:2]
我只需要从这个文件中转换一行。结果应该是这样一个文件:

<b>[source:1:2:3]</b>
<b>[source:2:3:1]</b>
<b>[result:3:1:2]</b>
<b>[source:1:3:2]</b>
[来源:1:2:3]
[来源:2:3:1]
[结果:3:1:2]
[来源:1:3:2]
在试图逃跑时摔碎了头[塞德:( 可能应该是这样的:

cat test.txt |sed "s,<b>\[source:3:1:2\]</b>,<b>\[result:3:1:2\]</b>,g"
cat test.txt | sed“s,\[来源:3:1:2\],\[结果:3:1:2\],g”
编辑:以查找特定字符串,然后更改行,请使用以下命令

awk -v old_string="source" -v new_string="result" '/<b>\[source:3:1:2\]<\/b>/{sub(old_string,new_string)} 1' Input_file
awk -v old_string="source" -v new_string="result" '/<b>\[source:3:1:2\]<\/b>/{sub(old_string,new_string)} 1' Input_file > temp && mv temp Input_file
awk 'FNR==3{sub(/source/,"result")} 1'  Input_file
awk -v old_string="source" -v new_string="result" 'FNR==3{sub(old_string,new_string)} 1' Input_file
或者,如果您希望在
awk
code中包含当前值和新值的变量,请尝试以下操作

awk -v old_string="source" -v new_string="result" '/<b>\[source:3:1:2\]<\/b>/{sub(old_string,new_string)} 1' Input_file
awk -v old_string="source" -v new_string="result" '/<b>\[source:3:1:2\]<\/b>/{sub(old_string,new_string)} 1' Input_file > temp && mv temp Input_file
awk 'FNR==3{sub(/source/,"result")} 1'  Input_file
awk -v old_string="source" -v new_string="result" 'FNR==3{sub(old_string,new_string)} 1' Input_file

不幸的是,文件中可能有更多的行,并且其中的数字可能不同。我只需要找到原始行并替换其中的一个单词。@Evgeny,很抱歉我没有理解,我们将如何在哪一行上进行替换?您能澄清一下吗?这是我们需要查找的字符串的数字吗?字符串和其中的数字是唯一的。我需要在行中找到一个具有特定值的字符串。在本例中,它是:[source:3:1:2]@Evgeny,请您现在尝试一下我的编辑代码,让我知道吗?是的,这正是我需要的!非常感谢!