2

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"

1 Answer 1

6

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"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Do you probably know how can result be rounded?
You are amazing... I do not have enough score to vote :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.