2017-06-20 5 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

はあなたにエラーがありますか? error.logファイルは、awkコマンドの戻りコードが0より大きいコマンドの場合にのみ設定されます。 –

+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コマンドの最後に、ファイルにstdoutをリダイレクトし、>2>を使用して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呼び出しの最後に2つのリダイレクトを追加します.1つはstdoutに、もう1つはwhateverlogfileに、もう1つはstderrに、もう1つはwhatevererrorlogに追加してください。 –

+0

あなたが入力ファイルを見ると、thing1の総コストは21294(7098 * 3)ですが、22394と言われています。したがって、私のログファイルは になります。 計算のエラー:計算のエラー: thing1の合計が間違っています:22394の代わりに21294 全体の合計が間違っています:40294 – User88

関連する問題