2

我有巨大的错误日志文件,显示数据加载时遇到的错误。

我需要报告不是唯一违反约束的错误,但是由于文件的大小,手动搜索文件是不切实际的。

日志文件:

Record 1: Rejected - Error on table DMT_.
ORA-00001: unique constraint (DM.DMT__PK) violated

Record 2: Rejected - Error on table DMT_.
ORA-01400:cannot insert NULL in to("DM"."DMT_INSURANCE"."INSURANCE_FUND_CODE")

Record 3: Rejected - Error on table DMT_.
ORA-00001: unique constraint (DM.DMT__PK) violated

Record 4: Rejected - Error on table DMT_ADDRESS, column ORIGINAL_POSTCODE.
ORA-12899: value too large for column "DM"."DMT_ADDRESS"."ORIGINAL_POSTCODE" (actual: 12, maximum: 10)

所需的输出文件是

Record 2: Rejected - Error on table DMT_.
ORA-01400:cannot insert NULL in to("DM"."DMT_INSURANCE"."INSURANCE_FUND_CODE")

Record 4: Rejected - Error on table DMT_ADDRESS, column ORIGINAL_POSTCODE.
ORA-12899: value too large for column "DM"."DMT_ADDRESS"."ORIGINAL_POSTCODE" (actual: 12, maximum: 10)

我很确定这可以在 grep、sed 或 awk 中完成,但我对这类事情不熟悉......我真的很感激一两个指针。

4

5 回答 5

2

使用grep. 您希望由以下人员生成的行:

grep -B1 "unique constraint.*violated" filename

现在从输入中消除这些行:

grep -v -f <(grep -B1 "unique constraint.*violated" filename) filename

你得到结果:

Record 2: Rejected - Error on table DMT_.
ORA-01400:cannot insert NULL in to("DM"."DMT_INSURANCE"."INSURANCE_FUND_CODE")


Record 4: Rejected - Error on table DMT_ADDRESS, column ORIGINAL_POSTCODE.
ORA-12899: value too large for column "DM"."DMT_ADDRESS"."ORIGINAL_POSTCODE" (actual: 12, maximum: 10

(这假设Record ...and位于不同的ORA-...行。如果它们位于同一行,grep -v "unique constraint.*violated" filename将起作用!)

于 2013-08-01T06:12:43.617 回答
2

如果你perl有空,你可以使用它的段落模式:

$ perl -00 -ne 'print unless /unique constraint/m;' < foo.input
Record 2: Rejected - Error on table DMT_.
ORA-01400:cannot insert NULL in to("DM"."DMT_INSURANCE"."INSURANCE_FUND_CODE")

Record 4: Rejected - Error on table DMT_ADDRESS, column ORIGINAL_POSTCODE.
ORA-12899: value too large for column "DM"."DMT_ADDRESS"."ORIGINAL_POSTCODE" (actual: 12, maximum: 10)

同样使用awk

$ awk -v RS= '!/unique constraint/' foo.input
Record 2: Rejected - Error on table DMT_.
ORA-01400:cannot insert NULL in to("DM"."DMT_INSURANCE"."INSURANCE_FUND_CODE")
Record 4: Rejected - Error on table DMT_ADDRESS, column ORIGINAL_POSTCODE.
ORA-12899: value too large for column "DM"."DMT_ADDRESS"."ORIGINAL_POSTCODE" (actual: 12, maximum: 10)
于 2013-08-01T06:13:21.510 回答
2

这可能对您有用(GNU sed):

sed '/^Record/{N;N;/\nORA-00001:/d}' logfile

为每条记录读取 3 行,如果这些行包含不需要的代码,请删除它们。

如果需要更多过滤,可以在封闭之前添加更多代码}

于 2013-08-01T06:47:56.657 回答
2

这是一个可能的解决方案,使用 Perl-regex(带有负前瞻)来排除 ORA-00001,然后也获取匹配 ORA 之前的行(-B1):

grep -B1 -P 'ORA\-(?!00001)' logfile
于 2013-08-01T06:24:42.297 回答
1

使用的一种方法。对于以读取下一个开头的每个字段Record并尝试匹配字符串unique ...。如果没有成功,请打印两者并添加换行符。

sed -n '/^Record/ { N; /unique constraint .* violated/! { s/$/\n/; p } }' infile

它产生:

Record 2: Rejected - Error on table DMT_.
ORA-01400:cannot insert NULL in to("DM"."DMT_INSURANCE"."INSURANCE_FUND_CODE")

Record 4: Rejected - Error on table DMT_ADDRESS, column ORIGINAL_POSTCODE.
ORA-12899: value too large for column "DM"."DMT_ADDRESS"."ORIGINAL_POSTCODE" (actual: 12, maximum: 10)
于 2013-08-01T06:10:47.797 回答