用sed注释掉多个文件中的一行

用sed注释掉多个文件中的一行,sed,replace,Sed,Replace,我需要注释掉一条路径上的一行多个文件。台词是 input_types = ['text'] 我需要用 #input_types = ['text'] 我想做一些像 sed -i 's/input_types/#input_types/g' path/to/files/* 但这会改变输入类型的所有实例,我不希望这样 我也试过了 sed -i 's/input_types = ['text']/#input_types = ['text']/g' path/to/files/* 但它不起作用

我需要注释掉一条路径上的一行多个文件。台词是

input_types = ['text']
我需要用

#input_types = ['text']
我想做一些像

sed -i 's/input_types/#input_types/g' path/to/files/*
但这会改变输入类型的所有实例,我不希望这样

我也试过了

sed -i 's/input_types = ['text']/#input_types = ['text']/g' path/to/files/*
但它不起作用


如何仅更改该特定实例?

您上次的尝试非常好,但必须更改两件事:

使用单引号括住表达式,但单引号也是表达式的一部分-这会让人困惑。在这种情况下,最好使用双引号将表达式括起来。 []括号必须用反斜杠转义:\[\] 因此,如果您将行更改为

sed -i "s/input_types = \['text'\]/#input_types = \['text'\]/g" /path/to/files/*

它应该可以工作。

您不需要重复完整的模式,您可以在替换字符串中使用&sed-i s/input\u types=\['text'\]/&/g/path/to/files/*。