You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用grep提取含pe=1的条目及后续至下一个>符号的内容?

提取含pe=1的完整条目

推荐方案:用awk处理

直接执行这条命令就能拿到你要的结果:

awk -v RS='>' '/pe=1/ {print ">"$0}' input.txt

备选方案:grep上下文匹配(仅适用于条目行数固定的情况)

如果能确定每个符合条件的条目首行之后最多有N行内容,可以用grep的-A参数指定要显示的后续行数,比如每个条目首行后最多2行:

grep -A 2 'pe=1' input.txt

但这个方法局限性大,条目行数变化时就失效,所以优先用awk方案。

命令说明

  • awk方案:
    • 用-v RS='>'把文本分割成以>开头的独立块,每个块对应一个完整条目。
    • 筛选出包含pe=1的块,输出时补上开头的>,还原原始格式。

执行结果

运行awk命令后,输出和你预期的完全一致:

>ab:xy_a0by98-2 \Movie= top gun \actor= Tom \Genere=Action \Length=234 \Credits=30 \pe=1 \summry=(Tom|action|234)
Top Gun is a 1986 American action drama film directed by Tony Scott, and produced by Don Simpson and Jerry Bruckheimer

>ab:xy_c0ma65-1 \Movie= Batman \actor= Bale \Genere=Action \Length=251 \Credits=30 \pe=1 \summry=(Bale|Action|251)
From American Psycho to Batman Begins to Vice, Christian Bale is a bonafide A-list star
But he missed out on plenty of huge roles along the way.

>ab:xy_e0ra81-2 \Movie= Superman \actor= henry cavill \Genere=Action \Length=254 \Credits=28 \pe=1 \summry=(cavill|action|254)
Henry William Dalgliesh Cavill is a British actor
He is known for his portrayal of Charles Brandon in Showtime's The Tudors

内容的提问来源于stack exchange,提问作者user2110417

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 15:36:08