更改CSV文件中的列值

时间:2015-11-04 08:55:47

标签: bash csv awk

在csv文件中我有以下列,我尝试用
更改第二列的值 awk -F ',' -v OFS=',' '$1 { $2=$2*2; print}' path/file.csv > output.csv
但它返回零并删除双引号。

FILE.CSV

"sku","0.47","supplierName"
"sku","3.14","supplierName"
"sku","3.56","supplierName"
"sku","4.20","supplierName"

output.csv

"sku",0,"supplierName"
"sku",0,"supplierName"
"sku",0,"supplierName"
"sku",0,"supplierName"

1 个答案:

答案 0 :(得分:5)

您可以在FS值中指定多个字符。

$ awk -v FS="\",\"" -v OFS="\",\"" '{$2=$2*2}1' file
"sku","0.94","supplierName"
"sku","6.28","supplierName"
"sku","7.12","supplierName"
"sku","8.4","supplierName"

如果你想要四舍五入到小数位,请尝试这个。

$ awk -v FS="\",\"" -v OFS="\",\"" '{$2=sprintf("%.2f",$2*2)}1' file
"sku","0.94","supplierName"
"sku","6.28","supplierName"
"sku","7.12","supplierName"
"sku","8.40","supplierName"