|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today! |
|
#1
|
|||
|
|||
|
What's the simplest way to get Awk to parse CSV input records such as
col1,"col2","col""3""","col,4" I.e. such that print $1"|"$2"|"$3"|"$4 yields col1|col2|col"3"|col,4 This one has had me stumped for ages! Any help greatly appreciated. Thanks. |
|
#2
|
|||
|
|||
|
Quote:
You definetly have to agree with the data supplier on the fixed structure of the CSV. You may find out yourself wasting your time trying to make a script work with data that come in everytime with a new hickup. try this: Code:
[zby@montana c1]$ echo "col1,\"col2\",\"col\"\"3\"\"\",\"col,4\"" | awk -F ",\"" '{print $1"|"$2"|"$3"|"$4}'|sed -e s/\"//g
col1|col2|col3|col,4
[zby@montana c1]$
|
|
#3
|
|||
|
|||
|
The field seperator variable
Set the FS to a comma, or whatever else you want to use, then deal with all the quoting headaches as patiently as possible.
$ cat test.data fred,george,sarah marcy,wallace,larry $awk '{print $3,$2,$1}' FS="," < test.data sarah george fred larry wallace marcy |
|
#4
|
|||
|
|||
|
What frood posts is true, of course, but if the data come in in sort of a "loose" format as chrisjj posted than all you can do is set the awk FS to: ," and hope it'll parse. Get rid of the quotes once sepparated.
There is also a recommendation how an average CSV file should look like. You may find below links helpful. Ifound them via google just to be sure before I start off running my mouth too much. ![]() http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm http://www.hi-tier.de/Entwicklung/Konzept/HITP/csvformat.html Anyways, this may be helpfull: Code:
[zby@montana c1]$ echo "col1,\"col2\",\"col\"\"3\"\"\",\"col,4\"" | awk -F ",\"" '{print $1"|"$2"|"$3"|"$4}'|sed -e s/\"//g
col1|col2|col3|col,4
[zby@montana c1]$
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Awk reading CSV |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|