Parse a csv using awk and ignoring commas inside a field
Solution 1:
gawk -vFPAT='[^,]*|"[^"]*"' '{print $1 "," $3}' | sort | uniq
This is an awesome GNU Awk 4 extension, where you define a field pattern instead of a field-separator pattern. Does wonders for CSV. (docs)
ETA (thanks mitchus): To remove the surrounding quotes, gsub("^\"|\"$","",$3)
; if there's more fields than just $3
to process that way, just loop through them.
Note this simple approach is not tolerant of malformed input, nor of some possible special characters between quotes – covering all of those would go beyond the scope of a neat one-liner.
Solution 2:
The extra output you're getting from csv.awk
is from demo code. It's intended that you use the functions within the script to do the parsing and then output it how you want.
At the end of csv.awk
is the { ... }
loop which demonstrates one of the functions. It's that code that's outputting the -> 2|
.
Instead most of that, just call the parsing function and do print csv[1], csv[2]
.
That part of the code would then look like:
{
num_fields = parse_csv($0, csv, ",", "\"", "\"", "\\n", 1);
if (num_fields < 0) {
printf "ERROR: %s (%d) -> %s\n", csverr, num_fields, $0;
} else {
# printf "%s -> ", $0;
# printf "%s", num_fields;
# for (i = 0;i < num_fields;i++) {
# printf "|%s", csv[i];
# }
# printf "|\n";
print csv[1], csv[2]
}
}
Save it as your_script
(for example).
Do chmod +x your_script
.
And cat
is unnecessary. Also, you can do sort -u
instead of sort | uniq
.
Your command would then look like:
./yourscript Buildings.csv | sort -u > floors.csv
Solution 3:
My workaround is to strip commas from the csv using:
decommaize () {
cat $1 | sed 's/"[^"]*"/"((&))"/g' | sed 's/\(\"((\"\)\([^",]*\)\(,\)\([^",]*\)\(\"))\"\)/"\2\4"/g' | sed 's/"(("/"/g' | sed 's/"))"/"/g' > $2
}
That is, first substitute opening quotes with "((" and closing quotes with "))", then substitute "(("whatever,whatever"))" with "whateverwhatever", then change all remaining instances of "((" and "))" back to ".
Solution 4:
You could try this awkbased csv paser:
http://lorance.freeshell.org/csv/