【发布时间】:2013-06-20 17:35:36
【问题描述】:
我目前正在尝试创建一个自动化流程来动态解析选择的特别大的日志文件 (25MB+),并通过 Java Servlet 将它们返回给用户。
由于这些日志的大小,我正在尝试执行 Linux 解析命令来检索与用户相关的部分,然后再将它们加载到内存中。这些部分可以分布在整个日志中。
我仍处于掌握正则表达式和文本解析工具(如 sed)的早期阶段,我希望有人能指出我当前问题的正确方向。
我有一个选择的日志引用一行中的特定项目(例如 KEY1),然后是关于该项目的未知数量的信息行。
然后日志将切换到下一项并重复
我正在尝试确定是否有任何基于 linux 的文本命令的组合,可以采用格式的文件
This is the first line and should not display
This is a section containing the text KEY1
Line 1
Line 2
Line 3
Line 4
This is a section containing the text KEY2
BadLine 1
BadLine 2
This is a second section containing the text KEY1
Line 5
Line 6
This is a section containing the text KEY3
BadLine 3
BadLine 4
BadLine 5
BadLine 6
This is a third section containing the text KEY1
Line 7
Line 8
Line 9
This is the last line
然后返回:
This is a section containing the text KEY1
Line 1
Line 2
Line 3
Line 4
This is a second section containing the text KEY1
Line 5
Line 6
This is a third section containing the text KEY1
Line 7
Line 8
Line 9
This is the last line
命令
sed -n '/KEY1/,/KEY2/p' file
完成了抓取第一部分的工作,但我很难找到一种通用的方法来提取我需要的所有内容。
任何帮助将不胜感激。
谢谢
-- 编辑--
2013/06/20 03:10:01 PM| FINE |S9180 |[Device] [ID:128]
foo
bar
foo
bar
------------------------------------------
foo
bar
------------------------------------------
2013/06/20 03:10:02 PM| FINE |S9180 |[Device] [ID:132]
Other foo
Other bar
------------------------------------------
Other foo
Other bar
Other foo
Other bar
------------------------------------------
2013/06/20 03:10:03 PM| FINE |S9180 |[Device] [ID:128]
foo
bar
------------------------------------------
foo
bar
foo
bar
------------------------------------------
foo
bar
为了澄清,这是我正在使用的格式。我正在尝试在日志中获取特定设备的所有信息。例如[ID:128] 键下的所有文本,但忽略 [ID:132] 下的部分 (或除 ID:128 之外的任何其他 id 因为没有特定的设备进入顺序)
【问题讨论】:
-
嘿,你能举一些合理的例子来说明 KEY1、KEY2 和 KEY3 的样子吗?
-
我已更新帖子以进行澄清。干杯。
标签: regex perl sed awk pattern-matching