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
    • 最新
    • 标签
主页 / unix / 问题 / 426249
Accepted
pigeon
pigeon
Asked: 2018-02-24 23:54:58 +0800 CST2018-02-24 23:54:58 +0800 CST 2018-02-24 23:54:58 +0800 CST

替换可能跨越两行的模式

  • 772

我正在尝试了解N选项在 Sed 编辑器中的工作方式。我的目标是在“file01”中将“系统管理员”更改为“桌面用户”,同时刹车线甚至最后一行。Sed 不会赶上最后一行,因为不会有下一行。另一个修改是必要的,例如添加:

sed 's/System Administrator/Desktop User/',但是这个和:

sed 'System\nAdministrator/Desktop\nUser/'以一种意想不到的方式切换(对我来说),这样一个命令在最后一行或最后两行停止工作。这发生在两者N之间或两者之间。我正在使用 GNU Sed,版本 4.4 。

#cat file01
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
Another line
And here we have: System Administrator's Group as well.
1.System Administrator's group.
2.System Administrators Group.
3.System Administrators Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
System Administrators Group.

案例 1 , 之后的两个命令N

# sed '
>N
>s/System\nAdministrator/Desktop\nUser/
>s/System Administrator/Desktop User/
> ' file01
The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Another line
And here we have: Desktop User's Group as well.
1.Desktop User's group.
2.System Administrators Group.
3.Desktop Users Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
Desktop Users Group.

案例2,sed 's/System Administrator/Desktop User/'之前N。

# sed '
> s/System Adminitrator/Desktop User/
> N
> s/System\nAdministrator/Desktop\nUser/
> ' file01
The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Another line
And here we have: System Administrator's Group as well.
1.Desktop User's group.
2.System Administrators Group.
3.Desktop Users Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
System Administrators Group.

这对我来说似乎很奇怪,并且无法弄清楚出了什么问题。[编辑]:更多细节。

我正在寻找用"Desktop User"替换"System Administrator " 。此外,如果一行以“系统”结尾而下一行以“管理员”开头,我会将它们相应地替换为“桌面”和“用户”。所有这些都取自一本书,但输出与书中所说的不符。我最终不知道出了什么问题。我发现描述我的问题的唯一世界是优先级,我道歉,看来我错了。

sed
  • 2 2 个回答
  • 167 Views

2 个回答

  • Voted
  1. Best Answer
    Kusalananda
    2018-02-25T00:29:39+08:002018-02-25T00:29:39+08:00

    这实际上与优先级(通常与操作员有关)没有任何关系,而是与发出命令的顺序有关。


    看一下问题中的第一个示例:

    N
    s/System\nAdministrator/Desktop\nUser/
    s/System Administrator/Desktop User/
    

    这将成对读取行并对其应用两个替换。如果该对中的第二行以System(具有Administrator以下第三行)结尾,那么它将无法检测到。这意味着当跨越奇偶线时,字符串不会被替换。

    看一下问题中的第二个示例(拼写更正):

    s/System Administrator/Desktop User/
    N
    s/System\nAdministrator/Desktop\nUser/
    

    这将更改当前行上的字符串,读取下一行并在中间用换行符更改字符串。 这不会更改带有完整字符串副本的奇数行(或仅带有字符串的奇数行System)。


    使用 GNU sed:

    :top
    N
    $s/System\(.\)Administrator/Desktop\1User/g
    b top
    

    该脚本循环并将文件的所有行读入模式空间。一旦它到达输入的最后一行,它就会全局执行替换,同时允许两个单词之间的任何字符(也可以使用\([ \n]\))而不是\(.\))。

    结果将是

    The first meeting of the Linux Desktop
    User's group will be held on Tuesday.
    Another line
    And here we have: Desktop User's Group as well.
    1.Desktop User's group.
    2.Desktop Users Group.
    3.Desktop Users Group.
    The first meeting of the Linux Desktop
    User's group will be held on Tuesday.
    Desktop Users Group.
    
    • 2
  2. don_crissti
    2018-02-25T05:28:01+08:002018-02-25T05:28:01+08:00

    当搜索可能跨越两行的模式时,与and - aka aN一起使用,以便在模式空间1中始终有两行:PDN;P;D cycle

    sed '$!N
    s/System\nAdministrator/Desktop\nUser/
    s/System Administrator/Desktop User/g
    P
    D' <infile
    

    请注意,这s/System\nAdministrator/Desktop\nUser/是gnu sed语法。便携你会做

    s/System\nAdministrator/Desktop\
    User/
    

    1:它从第 1-2 行开始,在 P;D 之后处理第 2-3 行,然后是第 3-4 行,依此类推...

    • 0

相关问题

  • Linux grep文件1中的内容在文件2中[重复]

  • 如何在第三个逗号后用条件grep行

  • 根据第一个逗号之前的匹配删除重复行数

  • 如何改进这个字符转换脚本?

  • 如何删除两行之间的单行

Sidebar

Stats

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

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve