gpt4 book ai didi

linux - grep -A 直到一个字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:40 26 4
gpt4 key购买 nike

假设我们有一个包含以下内容的文件:

chapter 1 blah blah
blah num blah num
num blah num blah
...
blah num
chapter 2 blah blah

我们想要 grep 这个文件,所以我们采取这些行来自 chapter 1 blah blahblah num(下一章之前的行)。

我们唯一知道的是

  1. 说明字符串chapter 1 blah blah
  2. 在那之后的某处还有另一行以chapter 开头

一个虚拟的方法是

grep -A <num> -i "chapter 1" <file>

足够大<num>所以整章都在里面。

最佳答案

sed -ne '/^chapter 1/,/^chapter/{/^chapter/d;p}' file

关于linux - grep -A <num> 直到一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29180929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com