Zin
Zin

Reputation: 149

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

Upvotes: 2

Views: 1081

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

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"

Upvotes: 6

Related Questions