我有两个文本文件如下。其中包括每个项目的数量,价格,总数和所有项目总数。
file1.txt
Date: 05-10-2016 Item1 5 1000 6000 Item2 7 1500 10500 Item3 4 2000 8000 Total: 24500
file2.txt
Date: 07-12-2016 Item1 7 750 5250 Item2 3 900 2700 Item3 8 1000 9000 Total: 16950
我试图通过添加文件中存在的单个项目价格来再次阅读每个文件和总计。如果总数不匹配,则应在error.txt
文件中写一条错误消息,明确说明数据存在问题的日期。
error.txt
Date: 05-10-2016 Item1 5 1000 6000 : Item total calculation is wrong. Total: 24500 : All items total calculation is wrong. Date: 07-12-2016 Item3 8 1000 9000 : Item total calculation is wrong. Total: 16950 : All items total calculation is wrong.
答案 0 :(得分:0)
awk '/Date/{d=$0;t=0;p=0;next;}
/Total/{if(t!=int($2)){if(p==0){print d; p=1;}; print $0,":","All items total calculation is wrong."};next;}
{k=int($2)*int($3);t+=k;if(k!=int($4)){if(p==0){print d; p=1;}; print $0,":","Item total calculation is wrong."}}' file1 file2 >error.txt
输出:
cat error.txt
Date: 05-10-2016
Item1 5 1000 6000 : Item total calculation is wrong.
Total: 24500 : All items total calculation is wrong.
Date: 07-12-2016
Item3 8 1000 9000 : Item total calculation is wrong.
Total: 16950 : All items total calculation is wrong.