From: Marcio Carneiro Date: 2011-10-08T03:26:33+09:00 Subject: Re: Read thru Csv file and store it in variables --002215b0316289b0b104aeb9947f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Re: How do I read, split and derive from CSV using bash script? http://fixunix.com/unix/83523-how-do-i-read-split-derive-csv-using-bash-scr= ipt.html On Sun, 25 Jan 2004 at 03:01 GMT, Phil Powell wrote: > I have a CSV file that I need to obtain values from... have absolutely > NO idea how to do it at all, except for: > > stuff=3D`cat ./directory_setup/directory_setup.csv` > > From there I'm lost. I need to then do the PHP equivalent of > fgetscsv() to get a specific column field from the parsed CSV file, or > even the TCL equivalent of split/list/split to parse it and retrieve > it. > > How is it done though in Bash? I am ultimately trying to dynamically > obtain the document root which is entered as a value in a CSV file > (have no other known way of ever knowing that in the scope of the > portable/scalable script I'm writing) This function splits a CSV line into its components, and stores them in an array called values: csv_split() { ## USAGE: csv_split CSV_RECORD local record=3D${1%"${CR}"} local right local vnum=3D0 unset values while [ -n "$record" ] do case $record in \"*) right=3D${record#*\",} value=3D${record%%\",*} values[$vnum]=3D${value#\"} ;; *) values[$vnum]=3D${record%%,*} right=3D${record#*,} ;; esac case $record in *,*) record=3D${right} ;; *) record=3D${record#\"} values[$vnum]=3D${record%\"} break;; esac vnum=3D$(( $vnum + 1 )) done } N=3D3 CR=3D$'\r' while IFS=3D read -r line do csv_split "$line" printf "%s\n" "${values[N]}" ## print field N (numbered from 0) done < ./directory_setup/directory_setup.csv -- Chris F.A. Johnson http://cfaj.freeshell.org =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D My code (if any) in this post is copyright 2004, Chris F.A. Johnson and may be copied under the terms of the GNU General Public License 2011/10/7 Robert Klemme > On Thu, Oct 6, 2011 at 1:24 AM, ideal one wrote: > > > Thanks for your Info, Sorry if i am not clear with my requirement. > > Let me clearly explain below: I am looking for DATA DRIVEN Scenario. > > > > I read CSV file and use that data in my code. ie If i have 10 rows, i > > will run my program 10 times. > > > i will have to iterate each time into my code and use different row > > values, its sort of Data Driven Automation. > > let me know if my requirement makes sense. > > You can directly assign fields to block parameters and have your code > inside the foreach loop: > > 13:27:13 Temp$ ./csv.rb data.csv > name=3DJames country=3DUSA > name=3DRobert country=3DGermany > name=3DMichael country=3DNeverland > 13:27:18 Temp$ cat data.csv > James,bar,USA > Robert,bbb,Germany > Michael,ignore,Neverland > 13:27:27 Temp$ cat -n csv.rb > 1 #!/bin/env ruby19 > 2 > 3 require 'csv' > 4 > 5 CSV.foreach ARGV.shift do |name, ign1, country| > 6 printf "name=3D%-30s country=3D%-30s\n", name, country > 7 end > 13:28:18 Temp$ > > Of course you can invoke any method from there as well. > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > > --=20 Analista de Sistemas *MBA em Log=C3=ADstica, Mobiliza=C3=A7=C3=A3o e Meio Ambiente/GETRAM =E2=80=93 Ger=C3=AAncia Execut= iva de Transportes e Mobiliza=C3=A7=C3=A3o* * * Provedor de Servi=C3=A7os na InterNet Aten=C3=A7=C3=A3o: Esta carta pode conter anexos no formato *ODF* (*Open Do= cument Format*)/*ABNT* (extens=C3=B5es *odt*, *ods*, *odp*, *odb*, *odg*). Antes d= e pedir os anexos em outro formato, voc=C3=AA pode instalar gratuita e livrem= ente o *BrOffice* (http://www.broffice.org) ou o seguinte Aditivo para Microsoft Office (R) (http://www.sun.com/software/star/odf_plugin/get.jsp). --002215b0316289b0b104aeb9947f--