csv文件中的bash算术运算

时间:2015-07-24 11:22:45

标签: bash csv awk sed

我有一些csv文件,我需要在第4和第5列中总计12%:

原始档案:

Destination,First Interval,Next Interval,First Price,Next Price
93,180,180,4.32210,4.32210,ADD=#60:50&0|SEQ=intNxN@priceN&#12.97&#30%
9370,180,180,4.50000,4.50000,ADD=#60:50&0|SEQ=intNxN@priceN&#13.50&#30%
9371,180,180,4.50000,4.50000,ADD=#60:50&0|SEQ=intNxN@priceN&#13.50&#30%
9372,180,180,4.50000,4.50000,ADD=#60:50&0|SEQ=intNxN@priceN&#13.50&#30%

期望的输出:

Destination,First Interval,Next Interval,First Price,Next Price,Formula
93,180,180,4.84075,4.84075,ADD=#60:50&0|SEQ=intNxN@priceN&#12.97&#30%
9370,180,180,5.04000,5.04000,ADD=#60:50&0|SEQ=intNxN@priceN&#13.50&#30%
9371,180,180,5.04000,5.04000,ADD=#60:50&0|SEQ=intNxN@priceN&#13.50&#30%
9372,180,180,5.04000,5.04000,ADD=#60:50&0|SEQ=intNxN@priceN&#13.50&#30%

通过awk或sed

执行此操作的任何建议

2 个答案:

答案 0 :(得分:3)

你可以试试下面的awk命令,

awk -F',' -v OFS="," '{$4=$4+((12/100)*$4);$5=$5+((12/100)*$5);}1' file

DEMO

答案 1 :(得分:1)

这可能是使用sedawk

的另一种选择
sed 's/,/\t/g' file | awk '{print $4+((12/100)*$4), "\t", $5+((12/100)*$5)}'