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

时间:2012-12-10 10:42:02

标签: 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            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_wheel.so trust use_uid”时这样做,而不是在指向我要编辑的确切行时。

5 个答案:

答案 0 :(得分: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

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 

因此,我们有效地匹配以包含字符串#的{​​{1}}开头并删除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 ,然后获取2个捕获组并替换为sufficient

\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

答案 1 :(得分:1)

perl -pi -e '$_=~s/^.//g if(/sufficient      pam_wheel.so trust use_uid/)' your_file

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

答案 2 :(得分:0)

使用awk你可以这样做:

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

OR(如果一开始只能有一个#):

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

答案 3 :(得分: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

答案 4 :(得分:0)

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

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

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