[英]Commenting out lines in a file using a bash script
我正在尝试在脚本中修改我的 linux 机器上的某个文件。 该文件是 /etc/pam.d/login 文件。 问题是文件的内容是,
# Prints the message of the day upon succesful login.
# (Replaces the `MOTD_FILE' option in login.defs)
# This includes a dynamically generated part from /run/motd.dynamic
# and a static (admin-editable) part from /etc/motd.
session optional pam_motd.so motd=/run/motd.dynamic noupdate
session optional pam_motd.so
我需要注释掉这个文件中的第二个会话行,但是当我进行字符串匹配时,结果如下(我正在使用 SED 为那些感兴趣的人这样做)。
# Prints the message of the day upon succesful login.
# (Replaces the `MOTD_FILE' option in login.defs)
# This includes a dynamically generated part from /run/motd.dynamic
# and a static (admin-editable) part from /etc/motd.
#session optional pam_motd.so motd=/run/motd.dynamic noupdate
#session optional pam_motd.so
因为第一行也符合条件。 我如何确保整行匹配“会话可选 pam_motd.so”,而不仅仅是其中的某个部分。
谢谢!
您需要在正则表达式中使用行开始 ( ^
) 和行结束 ( $
)。 喜欢:
sed -i '/^session optional pam_motd.so$/s/^/#/' /path/to/file
使用^
和$
锚点来限制您的匹配:
sed 's/\(^session optional pam_motd.so$\)/#\1/' file > file.new
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.