AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1173963
Accepted
eekfonky
eekfonky
Asked: 2019-09-14 05:54:50 +0800 CST2019-09-14 05:54:50 +0800 CST 2019-09-14 05:54:50 +0800 CST

sed 替换文件中的字符

  • 772

我需要替换一个字符/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

command-line sed text-processing
  • 1 1 个回答
  • 422 Views

1 个回答

  • Voted
  1. Best Answer
    terdon
    2019-09-14T06:03:57+08:002019-09-14T06:03:57+08:00

    正则表达式的黄金法则是:少即是多。始终尝试找到足以定位您的搜索字符串的最简单的表达式。因此,与其尝试匹配整行,不如寻找一个小而独特的字符串:

    $ 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。请注意使用-Efor 扩展正则表达式并使用\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,它会查找0 或多个空格,然后cifs.spnego是. 但是,您的线路是: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/
    

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

    • 8

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve