Parsing a CSV file on UNIX, but also processing data within ""

I am trying to parse a CSV file on UNIX using AWK or shell scripts. But I ran into a problem here. If the data is in quotation marks (","), I want to replace the comma (,) with a space and remove the quotation marks. In addition, such data may occur several times in the same record.

For example: consider this entry

20, Manchester, Barclays League, Hoog, 123, "95, some data",

The conclusion should be as follows:

20, Manchester, League Barclays, xyz, 123.95 some data,

How to do it b basic UNIX commands or scripts. Please help me with this ....

+3
source share
2 answers
<input.csv python -c \
'import csv,sys;f=csv.reader(sys.stdin);print '\
'("\n".join(",".join(entry.replace(",", " ") for entry in line) for line in f))'
+1
source

, sed :

sed -i '.orig' -e ':a' -e 's/^\([^"]*\)"\([^,"]*\)"\(.*\)$/\1\2\3/g' \
-e 's/^\([^"]*\)"\([^,"]*\),\([^"]*\)"\(.*\)$/\1"\2 \3"\4/;ta' file.csv
0

All Articles