Using csvformat
from csvkit:
$ csvformat -D ';' file | tr , . | csvformat -d ';'
ID,PRICE,QUANTITY,ARRIVAL
01299,41.5,1,0
26528,412.03,0,0
38080,2.35,0,0
38081,2.35,0,0
The above command changes the CSV delimiter from the default comma to semi-colon. It then changes all remaining commas into dots before changing the field delimiter back to comma.
Alternatively, convert the CSV to JSON using csvjson
and modify the data with jq
:
$ csvjson -I file | jq -r '(.[0] | keys_unsorted | @csv), (map(.PRICE |= sub(",";".") | [.[]] | @csv)[])'
"ID","PRICE","QUANTITY","ARRIVAL"
"01299","41.5","1","0"
"26528","412.03","0","0"
"38080","2.35","0","0"
"38081","2.35","0","0"
Pass this through csvformat
(with no arguments) if you want to remove any unneeded quotes.
The jq
expression first outputs the header by taking the keys from the first object in the input array. It then modifies the PRICE
field of each element (changes the comma into a dot) and formats the values as CSV for output.
The difference in effect between this second variation and the first is that we only ever affect the PRICE
column. Any embedded commas in any other field will be untouched.
For reference, the JSON document that jq
gets as input from csvjson
, given the CSV data in the question, would be
[
{"ID":"01299","PRICE":"41,5","QUANTITY":"1","ARRIVAL":"0"},
{"ID":"26528","PRICE":"412,03","QUANTITY":"0","ARRIVAL":"0"},
{"ID":"38080","PRICE":"2,35","QUANTITY":"0","ARRIVAL":"0"},
{"ID":"38081","PRICE":"2,35","QUANTITY":"0","ARRIVAL":"0"}
]