2013-08-01 95 views
2

我有巨大的錯誤日誌文件,顯示在dataload上遇到的錯誤。使用grep/sed/awk other在unix中過濾文件?

我需要報告不是唯一約束違規的錯誤,但由於它們的大小,手動搜索文件是不切實際的。

日誌文件:

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的完成,awk或者sed,但我是新來的這種的東西......我真的很感激一兩個指針。

回答

2

這裏是一個可能的解決方案使用Perl正則表達式(帶有負向視圖)排除ORA-00001,然後在匹配的ORA之前獲得該行(-B1):

grep -B1 -P 'ORA\-(?!00001)' logfile 
+0

絕對完美!感謝你和所有人! – Nimbocrux

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) 
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會工作)

2

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

$ 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 -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

這可能爲你工作(GNU SED):

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

讀3行對每個記錄,如果這些線路包含不需要的代碼刪除它們。

如果需要更多過濾,可在封閉}之前添加更多代碼。