尝试用单词实例注释掉文件中的行

Trying to comment out lines in a file with instance of a word

这可能又是一个超级简单的问题,我就是想不通。我正在编写一个脚本,该脚本需要注释掉包含单词 "NETWORKID" 的任何实例的文件的所有行。问题是,我的代码是在特定文件的所有行的开头放置一个“#”。 (/etc/profile)

我的是这样的:

grep -i "NETWORKID" /etc/profile | sed 's/^/#/'

有什么想法吗?提前致谢!

您不需要 grep,只需使用 `sed:

sed '/NETWORKID/s/^/#/' /etc/profile

使用 -i(something) 将更新文件并使用扩展名创建备份,例如

sed -i.bak -e 's/(.*NETWORKID.*)/#/' /etc/profile

将更新您的配置文件,并创建 运行 /etc/profile.bak

命令之前配置文件的副本