我有一个巨大的错误日志文件,显示在dataload上遇到的错误。
我需要报告非唯一约束违规的错误,但由于文件大小,手动搜索文件是不切实际的。
日志文件:
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来完成,但我是这种新事物......我真的很感激一两个指针。
答案 0 :(得分: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 ...
和ORA-...
在不同的行上。如果它们在同一行,grep -v "unique constraint.*violated" filename
就可以了!)
答案 1 :(得分: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)
答案 2 :(得分:2)
这是一个可能的解决方案,使用Perl-regex(具有负向前瞻)来排除ORA-00001,然后在匹配的ORA之前得到该行(-B1):
grep -B1 -P 'ORA\-(?!00001)' logfile
答案 3 :(得分:2)
这可能适合你(GNU sed):
sed '/^Record/{N;N;/\nORA-00001:/d}' logfile
为每条记录读取3行,如果这些行包含不需要的代码,则删除它们。
如果需要更多过滤,可以在封闭}
之前添加更多代码。
答案 4 :(得分:1)
使用sed的一种方法。对于以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)