gpt4 book ai didi

linux - 在 Bash 中解析带有开始和结束定界符的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:54 24 4
gpt4 key购买 nike

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

starthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
inserttexthere
endhere

是否有一种有效的方法可以将其从“starthere”解析为“endhere”,并返回中间的所有文本?例如。使用开始和结束定界符解析数据?

我目前的想法是用'starthere'分隔整个文本,然后用'endhere'分隔剩余的文本以返回中间文本。有没有更有效的方法?

我一直在研究 Batch 中的字符串解析,但似乎大多数内置工具都是用于单字符定界的。

最佳答案

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

关于linux - 在 Bash 中解析带有开始和结束定界符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34275974/

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