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

标签 linux bash shell scripting

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

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/

相关文章:

java - Forge MDK runClient 崩溃,可能是由于 LWJGL。我该如何修复它?

linux - 在单个 Linux 机器上模拟网络跃点

c - DESTDIR 和 PREFIX 的 make

bash - 使用 shell 脚本在远程服务器上执行命令

java - 从 java 运行猴子

linux - 初始化脚本 '/dev/tty: No such device or address' 重定向错误

linux - Linux 的 ipsec 开源

bash - 没有扩展的 shell 脚本的 ctags

linux - 一个用于计算按键排序的字段的平均值的单行?

linux - 是否有适用于 Linux 的 Clang mingw 交叉编译器