简体   繁体   中英

Changing column value in CSV file

In csv file I have below columns and I try to change the second column's value with
awk -F ',' -v OFS=',' '$1 { $2=$2*2; print}' path/file.csv > output.csv awk -F ',' -v OFS=',' '$1 { $2=$2*2; print}' path/file.csv > output.csv .
But it returns zero and removes double quotations.

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"

You may specify more than one character in FS value.

$ 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"

Try this if you want to round upto two decimal places.

$ 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM