Sed Grep字符串并删除第一个字符

Sed Grep字符串并删除第一个字符,sed,awk,grep,tr,Sed,Awk,Grep,Tr,我想在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

我想在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
编辑后的预期视图:

#%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行只需删除“#”,但我希望在搜索字符串“足够的pam_wheel.so trust use_uid”时执行此操作,而不是在指向我要编辑的确切行时执行此操作。

这是
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
sed '/sufficient \+pam_wheel.so \+trust \+use_uid/s/^#//' file
解释:

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 
因此,我们有效地匹配了从
开始的行,其中包含字符串
足够的\s+pam\u wheel.So trust use uid
并删除

注意:
-r
标志用于扩展regexp,对于您的
sed
版本,它可能是
-E
,因此请检查
man

如果要将更改存储回
文件
,请使用
-i
选项:

$ sed -ri 's/^#(.*sufficient\s+pam_wheel\.so trust use_uid.*)/\1/' file
如果列对齐很重要,则最多捕获
足够的
,然后获取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

这是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
sed '/sufficient \+pam_wheel.so \+trust \+use_uid/s/^#//' file
解释:

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 
因此,我们有效地匹配了从
开始的行,其中包含字符串
足够的\s+pam\u wheel.So trust use uid
并删除

注意:
-r
标志用于扩展regexp,对于您的
sed
版本,它可能是
-E
,因此请检查
man

如果要将更改存储回
文件
,请使用
-i
选项:

$ sed -ri 's/^#(.*sufficient\s+pam_wheel\.so trust use_uid.*)/\1/' file
如果列对齐很重要,则最多捕获
足够的
,然后获取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
使用awk,您可以执行以下操作:

awk -F '#' '{ if ($0 ~ /^#.*sufficient +pam_wheel.so trust use_uid/) {$1=""; print;} else print}'  infile
或者(如果一开始只能有一个):

使用awk,您可以执行以下操作:

awk -F '#' '{ if ($0 ~ /^#.*sufficient +pam_wheel.so trust use_uid/) {$1=""; print;} else print}'  infile
或者(如果一开始只能有一个):


这里有一种使用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
sed '/sufficient \+pam_wheel.so \+trust \+use_uid/s/^#//' file
结果:


这里有一种使用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
sed '/sufficient \+pam_wheel.so \+trust \+use_uid/s/^#//' file
结果:

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


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

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

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

awk 'index($0,"sufficient pam_wheel.so trust use_uid"){sub/^#/,"")}1' file > tmp && mv tmp file

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

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

awk 'index($0,"sufficient pam_wheel.so trust use_uid"){sub/^#/,"")}1' file > tmp && mv tmp file


Quote
但是我想在搜索字符串“足够的pam\u wheel.so trust use\u uid”时这样做“当指向我要编辑的确切行时,我不会这样做。
@sudo_O:感谢您指出这一点,尽管它仍然可以在一个简单的awk命令中完成。现在请检查编辑后的答案。更好的是,如果不打印文件中的所有行,您只打印匹配行,而不打印
不是很有用。@sudo\u O:谢谢,也解决了这个问题。好的,应该指出,使用
awk
需要重定向才能更改文件
awk'{…}'file>new_file
awk'{…}'file>tmp;mv tmp文件
Quote
,但我想在搜索字符串“足够的pam_wheel.so trust use_uid”时这样做,而不是在指向我要编辑的确切行时这样做
@sudo_O:感谢您指出这一点,尽管它仍然可以在一个简单的awk命令中完成。现在请检查编辑后的答案。更好的是,如果不打印文件中的所有行,您只打印匹配行,而不打印
不是很有用。@sudo\u O:谢谢,也解决了这个问题。好的,应该指出,使用
awk
需要重定向才能更改文件
awk'{…}'file>new_file
awk'{…}'file>tmp;mv tmp文件
另一种情况,如果我有文件:
AllowUsers support admin
如何使该文件成为:
#AllowUsers support admin
DeniedUsers root
实际上DeniedUsers root位于新行上,可以很好地替换包含AllowUsers的整行,我找到了解决方案我的自我:
sed-r's/(AllowUsers.*)/#\1\ndiedusers root/”
谢谢,@sudo\ONow我从看到你的代码中了解到,是的,这就是我应该做的。另一种情况是,如果我有文件:
AllowUsers support admin
如何将该文件设置为:
\AllowUsers support admin
DeniedUsers root
实际上DeniedUsers root位于新行上,可以很好地替换包含AllowUsers的整行为我自己找到解决方案:
sed-r的/(AllowUsers.*)/#\1\ndiedusers root/”
谢谢,@sudo\on我从看到你的代码中了解到,是的,我会这么做。