regex - 从文本文件中的重复范围模式中获取特定行

标签 regex sed awk pattern-matching range

提取和保存两个分隔符之间的文本文件部分的方法是什么。 我想提取以第一次出现的字符串 “TYPE A”以第一次出现的“TYPE E”结束的部分。

类似于:

Some text, blah, blah, blah : TYPE A  
line 1 of text of my interest  
line 2 more text of my interest  
line 3 text of interest: TYPE A  
line 4 more and more  
line 5 more, now: TYPE A  
line 6 here is: TYPE B  
line 7 more and more text of my interest ...   
line 8 and now: TYPE E  
line 9 blah blah

即我想将任何在这两个定界符之间 保存到另一个文件 - 第一个 TYPE A 包括所有后续的和第一个 TYPE E。它可以排除或包括这两者,因为我真的很想进一步处理介于两者之间的内容。 我想使用 sed 或我可以在 OSX 或 Linux 上找到的类似实用程序来执行此操作,但不能使用 Perl。

最佳答案

使用awk:

awk '/TYPE A/{p=1;next}/TYPE E/{print $0;p=0}p' inputFile > outputFile

输出:

$ awk '/TYPE A/{p=1;next}/TYPE E/{print $0;p=0}p' inputFile
line 1 of text of my interest
line 2 more text of my interest
line 3 more and more text of my interest ... : TYPE E

关于regex - 从文本文件中的重复范围模式中获取特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17372838/

相关文章:

bash - 无法分隔分号分隔线awk

ruby - 如何用 ruby​​ 获取包含汉字的整个字符串?

javascript - 浏览器是否支持不同的 HTML5 模式正则表达式功能?

php - 正则表达式在 PHP 中的工作方式与 C# 中的工作方式不同

regex - sed 替换每个数字后的字符

linux - 更改文本文件中的一行(如果存在)

linux - 如何在一行中重复数据的末尾放置序号?

arrays - 使用 awk 在 bash 中使用另一个有序数组模式对多行数组进行排序

linux - 如何使用 cat、sed、awk 或 cut 将列添加到 csv 文件中的特定位置?

algorithm - sed优化(基于较小数据集的大文件修改)