在Bash中解析带有开始和结束分隔符的字符串

时间:2015-12-14 20:26:14

标签: linux bash shell scripting

我有一个包含大量字符的文本文件,如下所示:

starthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
endhere

是否有一种有效的方法可以从' starthere'解析到了' endhere',返回中间的所有文字?例如。使用开始和结束分隔符解析数据?

我目前的想法是将整个文本划分为' starthere',然后将剩下的文字分隔为' endhere'返回中间文本。有没有更有效的方法呢?

我一直在寻找Batch中的字符串解析,但似乎大多数内置工具都是针对单字符划分。

1 个答案:

答案 0 :(得分:1)

awk救援!

$ awk '/endhere/{f=0} f; /starthere/{f=1}' text

inserttexthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere

或者,如果你想要分隔符

$ awk '/starthere/,/endhere/' text

starthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
endhere
相关问题