2017-06-20 96 views
0

[編輯,因爲發佈的問題是錯誤的] 我需要將此代碼的輸出保存在名爲'logfile'的文件中,並且在該日誌文件中只有計算錯誤。將錯誤消息重定向到日誌文件中bash

for f in *.log; do 
awk 'NF && NR>1 && $0!~/total:/ 
{ 
things_cost=$2*$3; 
overalltotal=(overalltotal!="")? overalltotal"+"things_cost : things_cost; 
if(things_cost!=$4) 
{ 
things_er[$1]=things_cost" instead of "$4 
} 
err_t+=$4; t+=things_cost; 
} 
$0~/total/ && err_t 
{ 
print "Error in calculations:"; 
for(i in things_er) 
{ 
print "Things total for "i" is wrong: it should be "things_er[i] 
} 
print "Overalltotal is wrong: It should be "t; next 
}1' "$f" tmpfile && mv tmpfile logfile 
done 

使用上面的代碼,錯誤在日誌文件中以非常不可讀的格式重複。我正在使用下面給出的輸入文件。我需要在日誌文件

Date: 01-01-2007 
    Sold  price total 
thing1 3 7098 22394 
thing2 2 6500 13000 
thing3 20 300 6000 
Overalltotal: 41394 
----------------------------------- 
Date: 04-01-2007 
    Sold  price total 
thing1 10 700 5000 
thing2 48 900 43200 
Overalltotal: 46020 

+0

你肯定有錯誤?只有awk命令返回代碼的命令大於0時,纔會填充error.log文件。 –

+0

我試圖在計算輸入文件到日誌文件時輸出錯誤。當我試圖直接將這個'for'循環的輸出重定向到'日誌文件'時,它不起作用。 – User88

+0

未設置的變量默認爲0;你可以在沒有條件的情況下直接寫'overalltotal + = things_cost'。 – chepner

回答

-1

你的awk代碼不輸出任何東西到stderr要打印的日期和錯誤,我猜你想有兩個標準錯誤和標準輸出輸出。要做到這一點,你必須在這裏做以下

(echo "test" | awk '{print $0; print $0." error" | "cat 1>&2" }') > log 2> err_log 

最重要的部分是,你有,你做的每一個錯誤打印後追加| "cat 1>&2"。在awk命令結束時,您必須使用>2>將stdout重定向到log文件和stderr到err_log文件。


這裏是它如何工作的:

echo "\ 
################################# 
Date: 01-01-2007 
thing1 3 7098 22394 
thing2 2 6500 13000 
thing3 20 300 6000 
-------------------------------- 
Date: 04-01-2007 
thing4 10 700 5000 
thing5 48 900 43200 
Overalltotal: 46020 
################################# 
total 89593" > test.csv 

(awk ' 
    !/^#/ && !/^total/ && $2~/[0-9]+/ && $3~/[0-9]+/ && $4~/[0-9]+/ { 
     line_cost = $2*$3 
     real_total += line_cost; 
     precomputed_total += $4; 
     if(line_cost!=$4) 
      errors[NR]="Error (product "$1": line " NR ") line cost is " line_cost" != " $4; 
     print "\t"$0; 
    } 
    /^total/ && length(errors)>0 { 
     print "\tErrors in item totals:" | "cat 1>&2"; 
     for(i in errors) 
      print "\t\t"errors[i] | "cat 1>&2"; 
     if (real_total != precomputed_total) 
     print "\tPre-computed total: "$2 | "cat 1>&2"; 
     print "\tOverall total based on items totals: "precomputed_total | "cat 1>&2"; 
     print "\tOverall total based on items price*quantity: "real_total | "cat 1>&2"; 
    }' test.csv) > log 2> err_log 

echo "Content of log file" 
cat log 
echo "Content of err_log file" 
cat err_log 

輸出如下:

Log: 
    thing1 3 7098 22394 
    thing2 2 6500 13000 
    thing3 20 300 6000 
    thing4 10 700 5000 
    thing5 48 900 43200 
Error log: 
    Errors in item totals: 
     Error (product thing1: line 3) line cost is 21294 != 22394 
     Error (product thing4: line 8) line cost is 7000 != 5000 
    Pre-computed total: 89593 
    Overall total based on items totals: 89594 
    Overall total based on items price*quantity: 90494 
+0

我想讓輸出重定向到一個文件。編輯過這個問題,我需要將錯誤信息打印到文件中。 – User88

+0

我沒有得到你需要的東西。因爲我在這裏給出的代碼會將輸出重定向到一個文件並將錯誤輸出到另一個。 你只需要做兩件事,在awk腳本中修改你的打印到: 'print「計算錯誤:」| 「cat 1> &2";' 然後在awk調用結束時添加兩個重定向,一個用於標準輸出,即''whateverlogfile',另一個用於stderr,即'2> whatevererrorlog'。 –

+0

如果你看輸入文件,在thing1的總成本中是21294(7098 * 3),而它被提到了22394.所以我的日誌文件將有: 計算錯誤:計算錯誤: 事物1的總數是錯誤的:它應該是21294而不是22394 總體錯誤:應該是40294 – User88

相關問題