2015-12-14 46 views
0

开始和结束定界符我有一个​​大的字符串中的字符的文本文件,像这样:解析字符串在Bash中

starthere 
inserttexthere 
inserttexthere 
inserttexthere 
inserttexthere 
inserttexthere 
inserttexthere 
endhere 

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

我目前的想法是用'starthere'分隔整个文本,然后用'endhere'分隔剩下的文本来返回中间文本。有没有更有效的方法来做到这一点?

我一直在研究字符串解析在批处理,但它似乎最内置的工具是单字符定界。

+0

http://www.grymoire.com/Unix/Sed.html#uh-35a – 4ae1e1

+0

当然你也可以使用一个循环纯粹的bash,但这不是一个有效的方式。 – 4ae1e1

+1

如果'sed'/ starthere /,/ endhere/{processing ...}'file'不适合你,那么你需要提供更多的示例数据。 – Kent

回答

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