多种模式匹配

时间:2016-10-10 23:36:37

标签: regex

我正在尝试在两个正则表达式之间打印文件部分。扭曲是第一个表达式可以是模式1,Error:或模式2 FAILED,最后一个表达式是固定模式(----------)。

文件部分示例:

Line 01

10-10-16 22:23:34 Error: Failed to update discount row [Customer ABC|£-336.95]
10-10-16 22:23:34 Error processing file information, Block 27.
----------------------------------------------------------------------
Line 05

Line 06

Line 07

10-10-16 22:26:52 Error: Failed to update discount row [Customer DEF|£-0.66]
10-10-16 22:26:52 Error processing file information, Block 37.
----------------------------------------------------------------------
Line 11

Line 12

Line 13

20161010 22:28:25 File 6 FAILED... FILE11.txt
----------------------------------------------------------------------
Line 16

Line 17

Line 18

10-10-16 22:31:30 Error: Failed to update discount row [Customer JKL|£-155.88]
10-10-16 22:31:30 Error processing file information, Block 3.
----------------------------------------------------------------------
Line 22

Line 23

Line 24

10-10-16 22:33:04 Error: Failed to update discount row [Customer MNO|£-12.65]
10-10-16 22:33:04 Error processing file information, Block 9.
Line 27

Line 28

Line 29

20161010 22:35:41 File 7 FAILED... FILE12.txt
----------------------------------------------------------------------

The output I'm looking for is below

10-10-16 22:23:34 Error: Failed to update discount row [Customer ABC|£-336.95]
10-10-16 22:23:34 Error processing file information, Block 27.
----------------------------------------------------------------------
10-10-16 22:26:52 Error: Failed to update discount row [Customer DEF|£-0.66]
10-10-16 22:26:52 Error processing file information, Block 37.
20161010 22:28:25 File 6 FAILED... FILE11.txt
----------------------------------------------------------------------

10-10-16 22:31:30 Error: Failed to update discount row [Customer JKL|£-155.88]
10-10-16 22:31:30 Error processing file information, Block 3.
----------------------------------------------------------------------
10-10-16 22:33:04 Error: Failed to update discount row [Customer MNO|£-12.65]
10-10-16 22:33:04 Error processing file information, Block 9.
20161010 22:35:41 File 7 FAILED... FILE12.txt
----------------------------------------------------------------------

我设法得到的是使用此命令的一个或另一个:

cat FILE* | sed -n '/Error/,/------/p'  >> ${TEMP}/err.tmp

cat FILE* | sed -n '/FAILED/,/------/p' >> ${TEMP}/err.tmp

但是我的输出不是文本出现在文件中的顺序:

10-10-16 22:23:34 Error: Failed to update discount row [Customer ABC|£-336.95]
10-10-16 22:23:34 Error processing file information, Block 27.
----------------------------------------------------------------------
10-10-16 22:26:52 Error: Failed to update discount row [Customer DEF|£-0.66]
10-10-16 22:26:52 Error processing file information, Block 37.
----------------------------------------------------------------------
10-10-16 22:31:30 Error: Failed to update discount row [Customer JKL|£-155.88]
10-10-16 22:31:30 Error processing file information, Block 3.
----------------------------------------------------------------------
10-10-16 22:33:04 Error: Failed to update discount row [Customer MNO|£-12.65]
10-10-16 22:33:04 Error processing file information, Block 9.
----------------------------------------------------------------------
20161010 22:28:25 File 6 FAILED... FILE11.txt
----------------------------------------------------------------------
20161010 22:35:41 File 7 FAILED... FILE12.txt
----------------------------------------------------------------------

任何帮助都会受到赞赏,因为我一直在网上寻找解决方案而且找不到解决方案。

1 个答案:

答案 0 :(得分:0)

awk救援!

$ awk '/Error/||/Failed/,/----/' file