bash:读取输入文件,执行计算,并将错误写入日志文件
我有两个文本文件如下。其中包括每个文件末尾的每个项目数量、价格、总计和所有项目总计bash:读取输入文件,执行计算,并将错误写入日志文件,bash,file,Bash,File,我有两个文本文件如下。其中包括每个文件末尾的每个项目数量、价格、总计和所有项目总计 file1.txt Date: 05-10-2016 Item1 5 1000 6000 Item2 7 1500 10500 Item3 4 2000 8000 Total: 24500 Date: 07-12-2016 Item1 7 750 5250 Item2 3 900 2700 Item3 8 1000 9000 Total: 16950 Date: 05-10-2016 Item1 5 1000
file1.txt
Date: 05-10-2016
Item1 5 1000 6000
Item2 7 1500 10500
Item3 4 2000 8000
Total: 24500
Date: 07-12-2016
Item1 7 750 5250
Item2 3 900 2700
Item3 8 1000 9000
Total: 16950
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.
file2.txt
Date: 05-10-2016
Item1 5 1000 6000
Item2 7 1500 10500
Item3 4 2000 8000
Total: 24500
Date: 07-12-2016
Item1 7 750 5250
Item2 3 900 2700
Item3 8 1000 9000
Total: 16950
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.
我试图通过添加文件中的单个项目价格来再次阅读每个文件和总计。当总数不匹配时,它应该在error.txt
文件中写入一条错误消息,明确说明哪些天的数据有问题
error.txt
Date: 05-10-2016
Item1 5 1000 6000
Item2 7 1500 10500
Item3 4 2000 8000
Total: 24500
Date: 07-12-2016
Item1 7 750 5250
Item2 3 900 2700
Item3 8 1000 9000
Total: 16950
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.
输出:
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.
对于*.txt中的f;do awk'NR>1{a[$1]=$2*$3}{print}{END{printf(“\n”);对于(i in f){print i,“计算中的错误”,a[i]}'$f“>tmpfile和mv tmpfile“$f”;请注意。如果您有其他信息要提供,例如您尝试过的代码,请单击帖子下的“”链接更新您的问题。详情请参阅。非常感谢。