gpt4 book ai didi

bash - 更改 CSV 文件中的列值

转载 作者:行者123 更新时间:2023-11-29 09:39:58 26 4
gpt4 key购买 nike

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

文件.csv

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

输出.csv

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

最佳答案

您可以在 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"

关于bash - 更改 CSV 文件中的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33517459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com