这是sed
工作:
$ sed -r 's/^#(.*sufficient\s+pam_wheel\.so trust use_uid.*)/\1/' file
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
auth sufficient pam_wheel.so trust use_uid
auth include system-auth
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
Regexplanation:
s/ # Substitute
^# # A line starting with a #
( # Start capture group
.* # Followed by anything
sufficient # Followed by the word sufficient
\s+ # Followed by whitespace
pam_wheel\.so trust use_uid # Followed by the literal string (escaped .)
.* # Followed by anything
) # Stop capture group
/ # Replace with
\1 # The first capture group
所以我们实际上是匹配的行开始与包含字符串sufficient\s+pam_wheel.so trust use_uid
和去除#
注意#
: -r
标志用于扩展正则表达式,它可能是-E
对于您的sed
版本,请检查man
。
如果你想保存更改回file
使用-i
选项:
$ sed -ri 's/^#(.*sufficient\s+pam_wheel\.so trust use_uid.*)/\1/' file
如果列aligment是很重要的,然后捕捉高达sufficient
,它让你获得2个捕获组,代之以后\1 \2
。
$ sed -r 's/^#(.*)(sufficient\s+pam_wheel\.so trust use_uid.*)/\1 \2/' file
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
auth sufficient pam_wheel.so trust use_uid
auth include system-auth
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
编辑:
与#AllowUsers support admin
替换字符串AllowUsers support admin
:
$ sed -r 's/^(AllowUsers support admin.*)/#\1/' file
#AllowUsers support admin
$ sed -r 's/^(DeniedUsers root.*)/#\1/' file
#DeniedUsers root
很好的例子,由于@sudo_O –
另一种情况下,如果我有一个文件: '的AllowUsers支持admin' 如何使该文件是: '#AllowUsers支持admin' 'DeniedUsers root' –
实际上 DeniedUsers根 是一条新的线路可能是好的更换整列W包含AllowUsers –