sed 替换文件中的字符

sed 替换文件中的字符

我需要替换/etc/request-key.conf

文件格式为;

###############################################################################
#
# Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
# Written by David Howells ([email protected])
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
###############################################################################


###############################################################################
#
# We can run programs or scripts
# - Macro substitutions in arguments:
#   %%...   %...
#   %o  operation name
#   %k  ID of key being operated upon
#   %t  type of key being operated upon
#   %d  description of key being operated upon
#   %c  callout info
#   %u  UID of requestor
#   %g  GID of requestor
#   %T  thread keyring of requestor (may be 0)
#   %P  process keyring of requestor (may be 0)
#   %S  session keyring of requestor (may be the user's default session)
#
################################################################################

#OP TYPE    DESCRIPTION CALLOUT INFO    PROGRAM ARG1 ARG2 ARG3 ...
#====== ======= =============== =============== ===============================
create  dns_resolver *      *               /sbin/key.dns_resolver %k
create  user    debug:*     negate      /bin/keyctl negate %k 30 %S
create  user    debug:*         rejected        /bin/keyctl reject %k 30 %c %S
create  user    debug:*         expired         /bin/keyctl reject %k 30 %c %S
create  user    debug:*         revoked         /bin/keyctl reject %k 30 %c %S
create  user    debug:loop:*    *       |/bin/cat
create  user    debug:*     *       /usr/share/keyutils/request-key-debug.sh %k %d %c %S
create  cifs.spnego *   *       /usr/sbin/cifs.upcall -c %k
create  dns_resolver    *   *       /usr/sbin/cifs.upcall %k
negate  *   *       *       /bin/keyctl negate %k 30 %S

所以我需要从倒数第三行开始;

create cifs.spnego * * /usr/sbin/cifs.upcall -c %k

到;

create cifs.spnego * * /usr/sbin/cifs.upcall -t %k

我努力了;

sed -i 's/^\(create cifs.spnego *cifs.upcall\) \(%k\)/\1 -t \2/' /etc/request-key.conf

-c但实际上我只需要用-t

答案1

正则表达式的黄金法则是:少即是多。总是尝试找到最簡單表达式足以定位您的搜索字符串。因此,不要尝试匹配整行,而是寻找一小段但独特的字符串:

$ grep -- -c file
create  cifs.spnego *   *       /usr/sbin/cifs.upcall -c %k

我将您的文件另存为,如您所见,文件中file只有一个 的案例。因此您所需要的只是(请注意,这将创建一个备份文件):-c-i.bak

sed -i.bak 's/-c/-t/' /etc/request-key.conf

如果您想更加谨慎,确保只匹配目标行而不先进行搜索,只需-c在以 开头的任何行上更改create cifs.spnego。请注意使用-E来扩展正则表达式并使用\s+(1 个或多个空格)而不是尝试写入多个空格:

sed -Ei.bak 's/^(create\s+cifs\.spnego.*cifs.upcall\s+)-c/\1 -t/' /etc/request-key.conf

由于您不需要在之后进行任何更改-c,因此没有理由尝试匹配它:少即是多


您的尝试失败的原因是,*在正则表达式中是乘数,意思是“0 或更多”。因此,当您有 时cifs.spnego *cifs.upcall,它会寻找cifs.spnego,然后0 个或更多空格,然后是cifs.upcall。然而,你的台词是:

create  cifs.spnego *   *       /usr/sbin/cifs.upcall  -t %k

要匹配它,您需要匹配cifs.spnego,然后匹配一个空格,然后匹配*,然后匹配更多空格,*然后匹配另一个 ,/usr/sbin/然后才有cifs.upcall。要匹配所有这些,您需要(您需要\*匹配字符*):

/^create  cifs.spnego \*   \*       cifs.upcall/

或者,因为少即是多,简单来说:

/^create  cifs.spnego .*cifs.upcall/

意思.*是“任何东西”。

相关内容