3 ms·
It would be so great if awk had a csv mode. For whatever reason (Excel), CSV seems to be the default text format for field oriented data. Maybe I’m dumb but I’
by doctor_eval 7y ago
It would be so great if awk had a csv mode. For whatever reason (Excel), CSV seems to be the default text format for field oriented data.
Maybe I’m dumb but I’ve never come up with a separator regex that is quite right.
- gpanders 7y agoFor _actually_ comma separated values, just use awk -F , '...' Is this not what you mean? Edit: This comment might be helpful to you: https://news.ycombinator.com/item?id=22110036 https://news.ycombinator.com/item?id=22110036
- doctor_eval 7y agoThe problem is that the field themselves can contain quotes ('"'), which escapes the comma. So the standard FS=/-f doesn't work properly. It looks like FPAT from your linked article is for gawk. Gawk is great, but it's not everywhere. Still - it's good to know. Thanks!
- dredmorbius 7y agoThe simple case is: FS = "," or: awk -F ',' <program> If you're working with CSV data that has quoted strings with embedded commas, FPAT is your friend: FPAT = "([^,]+)|(\"[^\"]+\")" See: https://www.gnu.org/software/gawk/manual/gawk.html#Splitting-By-Content https://www.gnu.org/software/gawk/manual/gawk.html#Splitting...
- doctor_eval 7y agojust want to say thanks for this, I haven't had to deal with CSV for years and now only a week after you posted this I needed it. brew install gawk good to go :)