gpt4 book ai didi

vim - 如何使用vim从文本中提取特定段落?

转载 作者:行者123 更新时间:2023-12-04 22:34:57 24 4
gpt4 key购买 nike

我试图从包含这种格式文本的大文件中多次提取测试

CL blahblahblah  
SP blahblahblah blahblahblah blahblahblah
DE blahblahblahblahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah
AB blahblahblah blahblahblah blahblahblah
blahblahblahblahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah
C1 blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
lahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
RP blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah
EM blahblahblah blahblahblah blahblahblah blahblahblah
NR blahblahblah blahblahblah blahblahblah blahblahblah
TC blahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah
Z9 blahblahblah blahblahblah blahblahblah blahblahblah
PU blahblahblah blahblahblah blahblahblah blahblahblah
PI blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah

我只对以 C1、AB、TI 开头的条目感兴趣,但有时这些条目跨越多行,并且它们后面的 XX 标记行并不总是相同的。有没有一种简单的方法可以只保留这些条目?
所以我剩下的文字应该是这样的:
TI blahblahblah  
AB blahblahblah b lah blahblah blah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
C1 blahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah
TI blah blah blah blah blah blah
AB blahblahblah blahblahblah blahblahblah blahblahblahblahblahblah blahblahblah blahblahblah blahblahblah blahblahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah
C1 blahblahblah blahblahblah blahblahblah blahblahblahblahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah

等等..

非常感谢!

最佳答案

我会做:

:$put='X' | 1,$-1g/^\(\s\|C1\|AB\|TI\)\@!/   ,/^\S/-d
:$d

这将执行以下操作:
  • 在末尾插入包含“X”的行
  • 对于除最后一行( 1,$-1 )以外的每一行,如果它以非空格开头且不以 C1、AB 或 TI 开头( g/pattern/ ),则删除( d )直到下一行不以空格开头 ,/pattern/ 不包括( - 很短对于 -1 )
  • 删除末尾的“X”行

  • 为了尝试您是否使用 Gvim:
  • 将此代码复制到剪贴板
  • Gvim 中的
  • 运行 :@+(从链接到剪贴板的 + 寄存器播放 Ex 命令)。

  • 我得到了什么:
    AB blahblahblah blahblahblah blahblahblah 
    blahblahblahblahblahblah blahblahblah blahblahblah
    blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
    blahblahblah blahblahblah blahblahblah
    C1 blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
    blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
    blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah
    lahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah

    关于vim - 如何使用vim从文本中提取特定段落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9291182/

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