Shell 如何替换文本模式下文件中的行

Shell 如何替换文本模式下文件中的行,shell,sed,Shell,Sed,在shell脚本(Almquist shell)中,我需要在注释文本模式之后替换下一行 # specific comment line if [ -d "/home/somewhere" ];then 我不能直接更改该行,因为if[-d…行可能会在该文件中出现多次,我只需要更改注释后的一行。 这可以通过sed实现吗?使用GNU sed: sed '/^# specific comment line$/{n;s/.*/foo/}' file 输出: # specific comment lin

在shell脚本(Almquist shell)中,我需要在注释文本模式之后替换下一行

# specific comment line
if [ -d "/home/somewhere" ];then
我不能直接更改该行,因为if[-d…行可能会在该文件中出现多次,我只需要更改注释后的一行。 这可以通过sed实现吗?

使用GNU sed:

sed '/^# specific comment line$/{n;s/.*/foo/}' file
输出:

# specific comment line foo #特定注释行 福
添加
-例如,
…{n;s/*/foo/;}'
,也可以在OSX(非GNU)sed上使用……)完美!谢谢。我在“#特定注释行”之后有一些变量文本。删除$作为分隔符适用于在初始注释文本之后有更多文本的行。