ruby gsub! problem

K

Kev Jackson

Hi all,

I need to escape / in a gsub!

eg

line = line.gsub!(///, '')

Unfortunately I don't know how to escape the / (forward slash) - the
data I'm munging has this in it (annoyingly)

Kev
 
J

Jeremy Tregunna

Hi all,

I need to escape / in a gsub!

eg

line = line.gsub!(///, '')

line.gsub!(/\//, '')
Unfortunately I don't know how to escape the / (forward slash) - the
data I'm munging has this in it (annoyingly)

Kev


!DSPAM:433ce04c757271272720660!

--
Jeremy Tregunna
(e-mail address removed)

"If debugging is the process of removing bugs, then programming must be
the process of putting them in." --Dykstra
 
W

William James

Kev said:
Hi all,

I need to escape / in a gsub!

eg

line = line.gsub!(///, '')

Unfortunately I don't know how to escape the / (forward slash) - the
data I'm munging has this in it (annoyingly)

Kev

If you use this method, you don't need to escape the slashes:

"09/30/05".sub( %r{(\d\d)/(\d\d)/(\d\d)}, '\3-\1-\2')

"09/30/05".sub( %r!(\d\d)/(\d\d)/(\d\d)!, '\3-\1-\2')

"09/30/05".sub( %r](\d\d)/(\d\d)/(\d\d)], '\3-\1-\2')
 
K

Kev Jackson

Another gsub/regex problem

I've got a load of data with newlines (\n) scattered throughout, I want
to remove all the newlines from this file, before

filename.each do |line| ... end

I'm playing with various combinations for gsub! and unfortunately I
can't strip all of them out

filename.to_s.gsub!(/\n/, '')

removes some of them, the problem is

...ahh

input.to_s.gsub!(/(.*)\n(.*)/,'\1\2')

nope still doesn't work

Any ideas - (i'm looking through the pickaxe1 book but can't find
anything) - off to google now

Kev
 
B

Brian Schröder

Hi all,

I need to escape / in a gsub!

eg

line =3D line.gsub!(///, '')

Unfortunately I don't know how to escape the / (forward slash) - the
data I'm munging has this in it (annoyingly)

Kev

Nicest way to escape slashes is using a different regexp marker. I.e.

line.gsub!(%r{/}, '')

also be carefull to write either

line.gsub!(/a/, 'b')

or

line =3D line.gsub(/a/, 'b')

because gsub! does return nil if no substitution occured. E.g.

"123".gsub!(/a/, 'b') # =3D> nil

hope to help,

Brian
 
J

James Edward Gray II

Another gsub/regex problem

I've got a load of data with newlines (\n) scattered throughout, I
want to remove all the newlines from this file, before

filename.each do |line| ... end

The typical Ruby idiom for this is:

File.foreach(filename) do |line|
line.chomp!

# ... processing code goes here ...
end

Is there some reason this doesn't work for you?

James Edward Gray II
 
K

Kev Jackson

The typical Ruby idiom for this is:

File.foreach(filename) do |line|
line.chomp!

# ... processing code goes here ...
end
I didn't ry it to be honest, the real problem was that the data (when
exported by another tool), has (\n) line endings at arbitrary places (ie
3 in a typical 'line' of data). So I really wanted to strip them out
before resplitting on the real line delimiter ';'. I actually worked it
out, I was trying filename.to_s.gsub!(/\n/, ''), but although ruby won't
complain about this, it won't operate on it, if instead I spli on my
delimiter ';' and then do a line.gsub!(/\n/,'') everything works fine

Thanks for the help

Is the idiom above more for processing multiple files (for each file,
for each line do some processing close) kind of thing?

Kev
 
C

Collins, Justin

------_=_NextPart_001_01C5C7E3.D3A52FE2
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

I'm guessing the reason it worked after splitting and not before is due =
to you not using a multiline regexp.
I believe in 1.8 regexp was changed to automatically be multi-line...if =
I recall correctly.
So it would have to be like gsub(/\n/m,"")

-Justin


-----Original Message-----
From: Kev Jackson [mailto:[email protected]]
Sent: Sun 10/2/2005 8:30 PM
To: ruby-talk ML
Subject: Re: ruby gsub! problem
=20
The typical Ruby idiom for this is:

File.foreach(filename) do |line|
line.chomp!

# ... processing code goes here ...
end
I didn't ry it to be honest, the real problem was that the data (when=20
exported by another tool), has (\n) line endings at arbitrary places (ie =

3 in a typical 'line' of data). So I really wanted to strip them out=20
before resplitting on the real line delimiter ';'. I actually worked it =

out, I was trying filename.to_s.gsub!(/\n/, ''), but although ruby won't =

complain about this, it won't operate on it, if instead I spli on my=20
delimiter ';' and then do a line.gsub!(/\n/,'') everything works fine

Thanks for the help

Is the idiom above more for processing multiple files (for each file,=20
for each line do some processing close) kind of thing?

Kev



------_=_NextPart_001_01C5C7E3.D3A52FE2--
 
B

Brian Schröder

I didn't ry it to be honest, the real problem was that the data (when
exported by another tool), has (\n) line endings at arbitrary places (ie
3 in a typical 'line' of data). So I really wanted to strip them out
before resplitting on the real line delimiter ';'. I actually worked it
out, I was trying filename.to_s.gsub!(/\n/, ''), but although ruby won't
complain about this, it won't operate on it, if instead I spli on my
delimiter ';' and then do a line.gsub!(/\n/,'') everything works fine

Thanks for the help

Is the idiom above more for processing multiple files (for each file,
for each line do some processing close) kind of thing?

Kev

filename.to_s.gsub!(/\n/, '') would replace all newlines in the
filename. That is certainly not what you want. You could do

string =3D File.read(filename).gsub(/\n/, '')

to get the content of the file without newlines

and

lines =3D File.read(filename).gsub(/\n/, '').split(";", -1)

to get what you want. The snippet from James is for processing each
line of a file after each other and may be a less memory intensive way
to achieve your goal.

hope to help,

Brian
 
B

Brian Schröder

Hi,

I'm guessing the reason it worked after splitting and not before is due t=
o you not using a multiline regexp.
I believe in 1.8 regexp was changed to automatically be multi-line...if I= recall correctly.
So it would have to be like gsub(/\n/m,"")

I don't think so. The /m option more or less only turns on that .
matches newline, so /\n/ and /\n/m would work alike.

regards,

Brian
 
K

Kev Jackson

filename.to_s.gsub!(/\n/, '') would replace all newlines in the
filename. That is certainly not what you want. You could do

string = File.read(filename).gsub(/\n/, '')

to get the content of the file without newlines

and

lines = File.read(filename).gsub(/\n/, '').split(";", -1)

to get what you want. The snippet from James is for processing each
line of a file after each other and may be a less memory intensive way
to achieve your goal.

hope to help,
Here's what I ended up with - love blocks, without them there'd be a lot
more resource handling...

File.open("sections.sql", "w") do |output|
File.open("TBLSECTION.dat","r") do |input|
lines=0
# here I was trying to remove the \n
input.to_s.gsub!(/\n/, '')
# didn't work so moved into munge_fields
input.each(";") do |line|
munge_fields line
munge_data(line, lines+1)
p line
output << line << "\n"
lines+=1
end
p "Total Lines in file : " + lines.to_s
end
end

both munge_fields and munge_data do the sub! thing to bring the data
back to the format I want, and although I considered splitting to get an
array of strings then creating a builder to rebuild my output, for now
it works fine with simple regexps

Kev
 
B

Brian Schröder


Here's what I ended up with - love blocks, without them there'd be a lot
more resource handling...

File.open("sections.sql", "w") do |output|
File.open("TBLSECTION.dat","r") do |input|
lines=3D0
# here I was trying to remove the \n
input.to_s.gsub!(/\n/, '')

Here your create a string from the input and substitute the newlines
against the empty string. You then forget about the string and it gets
garbage collected. Try

content =3D input.read
content.gsub!(/\n/, '')

and replace the below against

content.gsub!(/;/, ";\n")

and then write the content to a file

output << content
# didn't work so moved into munge_fields
input.each(";") do |line|
munge_fields line
munge_data(line, lines+1)
p line
output << line << "\n"
lines+=3D1
end
p "Total Lines in file : " + lines.to_s
end
end

both munge_fields and munge_data do the sub! thing to bring the data
back to the format I want, and although I considered splitting to get an
array of strings then creating a builder to rebuild my output, for now
it works fine with simple regexps

Kev

Or you do it in a more concise way:


File.open("sections.sql", "w") do |output|
output << File.read("TBLSECTION.dat").gsub(/\n/, '').gsub(/;/, ";\n")
end

hope to help,

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

gsub ? 2
gsub! on a txt file 3
Ruby GSUB question 6
gsub with wildcard 6
Gsub!("\n","\n") 9
Creating a gsub! method for Arrays 7
gsub pattern substitution and ${...} 7
why gsub! doesn't work in my script 7

Members online

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top