# Gsub help

**URL:** <https://www.ruby-forum.com/t/gsub-help/180163>\
**Category:** Ruby\
**Created:** [December 13, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/gsub-help/180163 "2009-12-13T21:34:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [December 13, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/gsub-help/180163/1 "2009-12-13T21:34:15Z")

</div>

I am reading in a file that is basically a csv file but the delimiter is  
spaces. I am also using fastercsv to parse the file into an array.  
Before fastercsv reads it I am substituting all spaces into commas with  
gsub  
l.gsub!(" ", “,”)  
I have one column in each row that could contain spaces. For example  
firstname,lastname,phonenumber,streetname,zip  
john crichton 555-555-1234 ^A street name with spaces^ 12345  
turns into  
john,crichton,555-555-1234,A,street,name,with,spaces,12345  
I can place the column that could contain spaces at the end of each row  
and then call like this but I would like to preserve those spaces.  
FasterCSV.parse(l) do |row|  
name=row[1]  
lastname=row[2]  
phone=row[3]  
zip=row[4]  
street=row[6…10]  
Each street name starts with a ^ and ends with a ^. Does anyone know if  
there is way I can replace all spaces up until the first ^ and then  
begin again after the last ^ so the street name is preserved with spaces  
and each column is separated by commas so my result set looks like the  
following?  
john,crichton,555-555-1234,A street name with spaces,12345

Thanks

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [December 13, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/gsub-help/180163/2 "2009-12-13T22:42:56Z")

</div>

Hi,

This isn’t particularly efficient or clever, but it should work:

“john crichton 555-555-1234 ^A street name with spaces^  
12345”.scan(/(\w+) (\w+) (\S+) ^(.+)^ (\d+)/).join ‘,’

- Rob

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [December 13, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/gsub-help/180163/3 "2009-12-13T23:39:25Z")

</div>

On Dec 13, 2:34 pm, Kvetch K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am reading in a file that is basically a csv file but the delimiter is  
> spaces. I am also using fastercsv to parse the file into an array.

Check out the docs for fastercsv - [http://fastercsv.rubyforge.org/](http://fastercsv.rubyforge.org/) -  
it looks like the FasterCSV class has default values for separator and  
quote characters. You should be able to change those to ’ ’ & ‘^’,  
respectively.

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [December 13, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/gsub-help/180163/4 "2009-12-13T23:49:25Z")

</div>

yermej wrote:

> On Dec 13, 2:34ï¿½pm, Kvetch K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am reading in a file that is basically a csv file but the delimiter is  
> > spaces. ï¿½I am also using fastercsv to parse the file into an array.
> 
> Check out the docs for fastercsv - [http://fastercsv.rubyforge.org/](http://fastercsv.rubyforge.org/) -  
> it looks like the FasterCSV class has default values for separator and  
> quote characters. You should be able to change those to ’ ’ & ‘^’,  
> respectively.

Exactly. Like this:

ar=FasterCSV.read( path\_to\_file,{ :col\_sep =\> " ", :quote\_char =\>‘^’ })

hth,

Siep

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [December 14, 2009, 4:03am UTC](https://www.ruby-forum.com/t/gsub-help/180163/5 "2009-12-14T04:03:34Z")

</div>

yermej wrote:

> On Dec 13, 2:34ï¿½pm, Kvetch K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am reading in a file that is basically a csv file but the delimiter is  
> > spaces. ï¿½I am also using fastercsv to parse the file into an array.
> 
> Check out the docs for fastercsv - [http://fastercsv.rubyforge.org/](http://fastercsv.rubyforge.org/) -  
> it looks like the FasterCSV class has default values for separator and  
> quote characters. You should be able to change those to ’ ’ & ‘^’,  
> respectively.

Thanks yermej. I wasn’t using the fastercsv separator because I was  
performing some actions on the data before I read it in with fastercsv.  
In the end it probably doesn’t matter but thank you for your help.

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [December 14, 2009, 4:44am UTC](https://www.ruby-forum.com/t/gsub-help/180163/6 "2009-12-14T04:44:42Z")

</div>

Siep K. wrote:

> yermej wrote:
> 
> > On Dec 13, 2:34ï¿½pm, Kvetch K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > I am reading in a file that is basically a csv file but the delimiter is  
> > > spaces. ï¿½I am also using fastercsv to parse the file into an array.
> > 
> > Check out the docs for fastercsv - [http://fastercsv.rubyforge.org/](http://fastercsv.rubyforge.org/) -  
> > it looks like the FasterCSV class has default values for separator and  
> > quote characters. You should be able to change those to ’ ’ & ‘^’,  
> > respectively.
> 
> Exactly. Like this:
> 
> ar=FasterCSV.read( path\_to\_file,{ :col\_sep =\> " ", :quote\_char =\>‘^’ })
> 
> hth,
> 
> Siep

Thanks Siep. I got it to work nicely. I appreciate you and Yernej’s  
help

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [December 14, 2009, 4:45am UTC](https://www.ruby-forum.com/t/gsub-help/180163/7 "2009-12-14T04:45:23Z")

</div>

yermej wrote:

> On Dec 13, 2:34ï¿½pm, Kvetch K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am reading in a file that is basically a csv file but the delimiter is  
> > spaces. ï¿½I am also using fastercsv to parse the file into an array.
> 
> Check out the docs for fastercsv - [http://fastercsv.rubyforge.org/](http://fastercsv.rubyforge.org/) -  
> it looks like the FasterCSV class has default values for separator and  
> quote characters. You should be able to change those to ’ ’ & ‘^’,  
> respectively.

Thanks Yermej. I got the quote characters to solve my problem.  
Appreciate it.

---

<div class="post-metadata">

**Author:** ![kvetch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85e7bf/32.png) [@kvetch](https://www.ruby-forum.com/u/kvetch)\
**Post date:** [December 14, 2009, 10:21am UTC](https://www.ruby-forum.com/t/gsub-help/180163/8 "2009-12-14T10:21:40Z")

</div>

2009/12/13 Siep K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Exactly. Like this:
> 
> ar=FasterCSV.read( path\_to\_file,{ :col\_sep =\> " ", :quote\_char =\>‘^’ })

You can omit brackets to make it look a little better:

ar = FasterCSV.read( path\_to\_file, :col\_sep =\> " ", :quote\_char =\>‘^’ )

or, maybe a bit more efficient because you read line by line:

CSV.foreach(path\_to\_file, :col\_sep =\> " ") do |row|  
…  
end

Note, in 1.9.\* FasterCSV has replaced CSV which is why you can use CSV  
there.

Kind regards

robert
