Csv文件列计算

时间:2015-11-04 10:43:12

标签: bash csv

在csv文件中我有以下列,我尝试用

更改第二列的值
$ awk -v FS="\",\"" -v OFS="\",\"" '{$2=sprintf("%.2f",$2*2)}1' file.csv

但是有些列包含必须保持不变的字符串。我知道了

"^[0-9][0-9]*$" 

会检查它是否是数字,但是如何在我的命令中将它合并?

FILE.CSV

"sku","NO price","supplierName"
"sku","3.14","supplierName"
"sku","3.56","supplierName"
"sku","4.20","supplierName"

1 个答案:

答案 0 :(得分:1)

您可以使用:

awk -v FS='","' -v OFS='","' '$2+0 == $2{$2=sprintf("%.2f",$2*2)}1' file.csv
"sku","NO price","supplierName"
"sku","6.28","supplierName"
"sku","7.12","supplierName"
"sku","8.40","supplierName"

$2+0 == $2检查确保$2中的值为数字。