0

当 grep 某个字符串时,我想删除文件中的第一个字符。但是需要在编辑之前更改的行位于同一位置。

示例文件:

#%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

编辑后的预期视图:

#%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

在这种情况下,第 4 行只需要删除“#”,但我想在搜索字符串“sufficient pam_wheel.so trust use_uid”时这样做,而不是在指向我要编辑的确切行时。

4

5 回答 5

4

这是一份工作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

正则说明:

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

如果列对齐很重要,则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
于 2012-12-10T10:46:30.420 回答
1
perl -pi -e '$_=~s/^.//g if(/sufficient      pam_wheel.so trust use_uid/)' your_file

注意:这将进行就地替换。因此在您运行命令后,您的文件将被自动修改。

于 2012-12-10T12:34:21.177 回答
0

使用 awk 你可以:

awk -F '#' '{ if ($0 ~ /^#.*sufficient +pam_wheel.so trust use_uid/) {$1=""; print;} else print}'  infile

或(如果开头只能有一个 #):

awk -F '#' '{ if ($0 ~ /^#.*sufficient +pam_wheel.so trust use_uid/) print $2; else print}'  infile
于 2012-12-10T10:59:30.533 回答
0

这是一种使用方法sed

sed '/sufficient \+pam_wheel.so \+trust \+use_uid/s/^#//' 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
于 2012-12-10T11:09:36.303 回答
0

sed 命令只能进行 RE 匹配而不是字符串比较,因此要使用 sed,您需要解析所需的字符串以转义所有容易出错的 RE 元字符(例如,当前发布的解决方案都忽略了转义 "." in pam_wheel.so)。

当您尝试匹配字符串时,最好只进行字符串比较,例如:

awk 'index($0,"sufficient pam_wheel.so trust use_uid"){sub/^#/,"")}1' file > tmp && mv tmp file
于 2012-12-10T13:55:46.103 回答