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 / 问题 / 787693
Accepted
learningregularexpressions
learningregularexpressions
Asked: 2024-12-06 02:38:40 +0800 CST2024-12-06 02:38:40 +0800 CST 2024-12-06 02:38:40 +0800 CST

每次搜索结果匹配时打印特定部分

  • 772

我在 Linux 机器上有一个非常基本的文本文件,其中包含章节、对话和参考资料等内容。

这就是它的样子


Chapter: 1 One: Birds and Trees

Birds are beautiful and trees are amazing and
they are dependent on each other. Birds most of the time
choose to make their nests on trees since trees provide more
stability. One day the bird sat on a tree and said;

Bird: Oh my I'm so tired from all the flying, I should take a rest
Tree: Mr Bird, you seem tired, perhaps you should take some rest, and
here are some fruits to quench your thirst.
Bird: Oh thank you very much!

Reference:               Chapter 1: birds and trees

Chapter: 2 Two: Trees and Fruits

Fruits are very delicious to eat and they are mostly found
in trees. Fruits contain essential vitamins, minerals and loads
of good fibers.

Reference:               Chapter 2: trees and fruits

这些是 txt 文件的内容。现在假设我正在搜索quench,我认为它会从章节号开始直到参考。所以我尝试使用 grep;

$ grep -A 5 -B 5 'quench' file.txt

但是,这并没有产生所需的输出。我期望的是这样的;

Chapter: 1 One: Birds and Trees

Birds are beautiful and trees are amazing and
they are dependent on each other. Birds most of the time
choose to make their nests on trees since trees provide more
stability. One day the bird sat on a tree and said;

Bird: Oh my I'm so tired from all the flying, I should take a rest
Tree: Mr Bird, you seem tired, perhaps you should take some rest, and
here are some fruits to quench your thirst.
Bird: Oh thank you very much!

Reference:               Chapter 1: birds and trees

并且,搜索单词“维生素”会打印出来;

Chapter: 2 Two: Trees and Fruits

Fruits are very delicious to eat and they are mostly found
in trees. Fruits contain essential vitamins, minerals and loads
of good fibers.

Reference:               Chapter 2: trees and fruits

我想知道这是否可以通过 sed 或 awk 实现。

PS:每一行都是真正的新行

shell-script
  • 2 2 个回答
  • 49 Views

2 个回答

  • Voted
  1. Best Answer
    markp-fuso
    2024-12-06T03:25:49+08:002024-12-06T03:25:49+08:00

    一个awk想法:

    awk -v word="quench" '                                            # set variable "word" to string we are searching for
    /^Chapter:/      { inblock = 1; block = "" }                      # start of new block; set flag and clear block
    
    inblock          { block = block (block == "" ? "" : ORS) $0 }    # if inblock != 0 then append current line to "block"
    
    /^Reference:/ &&                                                  # if end of block and ...
    block ~ word     { print block; inblock = 0 }                     # word is in block then print block; clear flag
    ' file.txt
    

    这样-v word="quench"就生成了:

    Chapter: 1 One: Birds and Trees
    
    Birds are beautiful and trees are amazing and
    they are dependent on each other. Birds most of the time
    choose to make their nests on trees since trees provide more
    stability. One day the bird sat on a tree and said;
    
    Bird: Oh my I'm so tired from all the flying, I should take a rest
    Tree: Mr Bird, you seem tired, perhaps you should take some rest, and
    here are some fruits to quench your thirst.
    Bird: Oh thank you very much!
    
    Reference:               Chapter 1: birds and trees
    

    这样-v word="essential"就生成了:

    Chapter: 2 Two: Trees and Fruits
    
    Fruits are very delicious to eat and they are mostly found
    in trees. Fruits contain essential vitamins, minerals and loads
    of good fibers.
    
    Reference:               Chapter 2: trees and fruits
    

    使用-v word="bubble",或未-v word=...提供任何子句时,将生成:

    -- no output
    
    • 2
  2. jubilatious1
    2024-12-06T18:41:15+08:002024-12-06T18:41:15+08:00

    使用Raku(以前称为 Perl_6)

    ~$ raku -e 'my @a; 
                for slurp() { 
                    @a = .comb(/^^ Chapter  .*?  <?before \nChapter | $ > /) 
                };  @a.grep(/quench/).put;'  file
    

    某些人会发布 Perl 答案,但这里有一个用 Raku(又名 Perl6)编写的答案。Raku 内置了对 Unicode 的高级支持。

    slurp简而言之,输入文件,然后comb遍历以找到匹配的记录(章节)。然后在最后的语句中grep仅返回匹配的记录(章节)。示例输入与 OP 提供的相同。

    示例输出:

    Chapter: 1 One: Birds and Trees
    
    Birds are beautiful and trees are amazing and
    they are dependent on each other. Birds most of the time
    choose to make their nests on trees since trees provide more
    stability. One day the bird sat on a tree and said;
    
    Bird: Oh my I'm so tired from all the flying, I should take a rest
    Tree: Mr Bird, you seem tired, perhaps you should take some rest, and
    here are some fruits to quench your thirst.
    Bird: Oh thank you very much!
    
    Reference:               Chapter 1: birds and trees
    
    

    根据需要,在最后的语句中添加对trim、trim-leading或 的调用以删除周围的空格。trim-trailing

    https://raku.org

    • 0

相关问题

  • 在awk中的两行之间减去相同的列

  • 打印文件行及其长度的脚本[关闭]

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 按分隔符拆分并连接字符串问题

  • MySQL Select with function IN () with bash array

Sidebar

Stats

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

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

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

    • 4 个回答
  • Marko Smith

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

    • 5 个回答
  • Marko Smith

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

    • 3 个回答
  • 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
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +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

热门标签

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