# How to manage Tab & Comma separated filds form single text f

**URL:** <https://www.ruby-forum.com/t/how-to-manage-tab-comma-separated-filds-form-single-text-f/172016>\
**Category:** Rails\
**Created:** [July 13, 2009, 1:11pm UTC](https://www.ruby-forum.com/t/how-to-manage-tab-comma-separated-filds-form-single-text-f/172016 "2009-07-13T13:11:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhupsi1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bhupsi1982](https://www.ruby-forum.com/u/bhupsi1982)\
**Post date:** [July 13, 2009, 1:11pm UTC](https://www.ruby-forum.com/t/how-to-manage-tab-comma-separated-filds-form-single-text-f/172016/1 "2009-07-13T13:11:56Z")

</div>

Hi,

I am modifying on script. I am using this script to import data form  
text-delimited file into PosgresSql database.

I have already setup a code for tab separation but facing problem in  
comma separation. In text-delimited file there is foulr field which are  
inter related.  
first “organization & City” and Second “State & Country”. Where  
organization & City are separated with commas ", " and State & Country.  
But when the script will execute it will split the not only tab  
separated fields but also as and when he found comma separated field  
i.e.organization & City then it will split the Organization & city and  
stored it the respected field.

Could anybody please help me in this\> I have no such exposure to Ruby. I  
have here attached the data\_importer.rb file for your reference also  
need help here pasted code.

============================================================  
f.read.each do |l|  
if counter \> 0  
tokens = l.split(/\t/).collect{|ss| ss.delete(""") }  
hn = tokens[0]  
pgn = tokens[1]  
sn = tokens[2]  
pio = tokens[3]  
dio = tokens[4]  
org = tokens[5]  
al1 = tokens[6]  
al2 = tokens[7]  
ci = tokens[8]  
st = tokens[9]  
pc = tokens[10]  
co = tokens[11]  
te = tokens[12]  
fax = tokens[13]  
ew = tokens[14]  
eo = tokens[15]  
wa = tokens[16]  
at1 = tokens[17]  
at2 = tokens[18]  
at3 = tokens[19]  
c1 = tokens[20]  
c2 = tokens[21]  
c3 = tokens[22]

```
      begin

```

======================================

Regards,

Bhupendra

---

<div class="post-metadata">

**Author:** ![bhupsi1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bhupsi1982](https://www.ruby-forum.com/u/bhupsi1982)\
**Post date:** [July 13, 2009, 1:34pm UTC](https://www.ruby-forum.com/t/how-to-manage-tab-comma-separated-filds-form-single-text-f/172016/2 "2009-07-13T13:34:50Z")

</div>

Please find more below more hints on what the code is :

============================================  
begin  
f.read.each do |l|  
if counter \> 0  
tokens = l.split(/\t/).collect{|ss| ss.delete(""") }  
#tokens1 = l.split(/, /)  
#tokens2 = l.split(/, /)  
#tokens[5] = l.split(/, /)  
hn = tokens[0]  
pgn = tokens[1]  
sn = tokens[2]  
pio = tokens[3]  
dio = tokens[4]  
#tokens[5] = l.split(/, /)  
org = tokens[5]  
org1 = l.find(’, ')  
ci = l[:org1]  
al1 = tokens[6]  
al2 = tokens[7]  
ci = tokens[8]  
st = tokens[9]  
pc = tokens[10]  
co = tokens[11]  
te = tokens[12]  
fax = tokens[13]  
ew = tokens[14]  
eo = tokens[15]  
wa = tokens[16]  
at1 = tokens[17]  
at2 = tokens[18]  
at3 = tokens[19]  
c1 = tokens[20]  
c2 = tokens[21]  
c3 = tokens[22]

```
      begin
        cn = Contact.find(:first, :conditions => 

```

# [“preferred\_given\_names = ? and surname = ? and (email\_work = ? OR email\_work IS NULL)”, pgn, sn, ew]) found = true #found = false if ! cn found = false cn = Contact.new cn.honorific = hn cn.preferred\_given\_names = pgn cn.surname = sn cn.position\_in\_organisation = pio cn.division\_in\_organisation = dio cn.organisation = org cn.address\_line\_1 = al1 cn.address\_line\_2 = al2 cn.city = ci cn.state = st cn.postal\_code\_zip = pc cn.country = co cn.phone = te cn.fax = fax cn.email\_work = ew cn.email\_other = eo cn.web\_address = wa cn.affiliation\_type = at1 cn.affiliation\_type2 = at2 cn.affiliation\_type3 = at3 cn.created = Time.now cn.modified = Time.now cn.save end

Bhupendra Chaudhari wrote:

> Hi,
> 
> I am modifying on script. I am using this script to import data form  
> text-delimited file into PosgresSql database.
> 
> I have already setup a code for tab separation but facing problem in  
> comma separation. In text-delimited file there is foulr field which are  
> inter related.  
> first “organization & City” and Second “State & Country”. Where  
> organization & City are separated with commas ", " and State & Country.  
> But when the script will execute it will split the not only tab  
> separated fields but also as and when he found comma separated field  
> i.e.organization & City then it will split the Organization & city and  
> stored it the respected field.
> 
> Could anybody please help me in this\> I have no such exposure to Ruby. I  
> have here attached the data\_importer.rb file for your reference also  
> need help here pasted code.
> 
> ============================================================  
> f.read.each do |l|  
> if counter \> 0  
> tokens = l.split(/\t/).collect{|ss| ss.delete(""") }  
> hn = tokens[0]  
> pgn = tokens[1]  
> sn = tokens[2]  
> pio = tokens[3]  
> dio = tokens[4]  
> org = tokens[5]  
> al1 = tokens[6]  
> al2 = tokens[7]  
> ci = tokens[8]  
> st = tokens[9]  
> pc = tokens[10]  
> co = tokens[11]  
> te = tokens[12]  
> fax = tokens[13]  
> ew = tokens[14]  
> eo = tokens[15]  
> wa = tokens[16]  
> at1 = tokens[17]  
> at2 = tokens[18]  
> at3 = tokens[19]  
> c1 = tokens[20]  
> c2 = tokens[21]  
> c3 = tokens[22]
> 
> ```
> begin
> 
> ```
> 
> ======================================
> 
> Regards,
> 
> Bhupendra
