Short solution for replacing Strings inside a file?

E

Emmanuel

Hello, i'm working with ruby 1.8.5 on a Suse SLES 9 Linux machine.

I need to replace strings inside files, specifically the string
FECHAIMPRE
with the current date. I had a bash script that looked like this:

BOF#################################################################
date=`date '+%d:%m:%y'`
for item in `ls /u/sag/impresion/PRODISAA/etiquetas/lpt28*.tmp`
do
sed -e s/FECHAIMPRE/$date/g < $item > $item.po
mv $item.po $item
done
EOF#################################################################


And i wanted to rewrite it in ruby. I came up with this:


#BOF#################################################################
$fecha= Time.now.strftime("%d/%m/%Y")

def UpdateDates(file)

filename= file.to_s

# Rename "file" to "file.temp"
FileUtils.mv(filename, filename + ".temp")

# Create "file" file
File.open(filename, "w") do |out|

# Write in "file" the modified "file.temp" lines.
IO.foreach(filename + ".temp") do |line|

if line =~ /FECHAIMPRE/
line.gsub!(/FECHAIMPRE/, $fecha)
end

out << line
end

end

# Erase the temporal
FileUtils.rm(filename + ".temp", :force => true)
end

Dir.glob("*.tmp").each { |file| UpdateDates(file) }

#EOF#################################################################

But i keep thinking there has to be a shorter way. I read the ruby
documentation
for IO and File classes but i haven't found any better solution.

What do you think about it???

Thanks!
 
R

Robert Klemme

Hello, i'm working with ruby 1.8.5 on a Suse SLES 9 Linux machine.

I need to replace strings inside files, specifically the string
FECHAIMPRE
with the current date. I had a bash script that looked like this:

BOF#################################################################
date=`date '+%d:%m:%y'`
for item in `ls /u/sag/impresion/PRODISAA/etiquetas/lpt28*.tmp`
do
sed -e s/FECHAIMPRE/$date/g < $item > $item.po
mv $item.po $item
done
EOF#################################################################


And i wanted to rewrite it in ruby. I came up with this:


#BOF#################################################################
$fecha= Time.now.strftime("%d/%m/%Y")

def UpdateDates(file)

filename= file.to_s

# Rename "file" to "file.temp"
FileUtils.mv(filename, filename + ".temp")

# Create "file" file
File.open(filename, "w") do |out|

# Write in "file" the modified "file.temp" lines.
IO.foreach(filename + ".temp") do |line|

if line =~ /FECHAIMPRE/
line.gsub!(/FECHAIMPRE/, $fecha)
end

out << line
end

end

# Erase the temporal
FileUtils.rm(filename + ".temp", :force => true)
end

Dir.glob("*.tmp").each { |file| UpdateDates(file) }

#EOF#################################################################

But i keep thinking there has to be a shorter way. I read the ruby
documentation
for IO and File classes but i haven't found any better solution.

What do you think about it???

Thanks!

untested so try it out with another file because -i.bak does in place
replacement.

ruby -i.bak -p -e "gsub! /FECHAIMPRE/, `date '+%d:%m:%y'`"
/u/sag/impresion/PRODISAA/etiquetas/lpt28*.tmp

Kind regards

robert
 
S

Stefano Crocco

Alle 15:39, luned=C3=AC 29 gennaio 2007, Emmanuel ha scritto:
Hello, i'm working with ruby 1.8.5 on a Suse SLES 9 Linux machine.

I need to replace strings inside files, specifically the string
FECHAIMPRE
with the current date. I had a bash script that looked like this:

BOF#################################################################
date=3D`date '+%d:%m:%y'`
for item in `ls /u/sag/impresion/PRODISAA/etiquetas/lpt28*.tmp`
do
sed -e s/FECHAIMPRE/$date/g < $item > $item.po
mv $item.po $item
done
EOF#################################################################


And i wanted to rewrite it in ruby. I came up with this:


#BOF#################################################################
$fecha=3D Time.now.strftime("%d/%m/%Y")

def UpdateDates(file)

filename=3D file.to_s

# Rename "file" to "file.temp"
FileUtils.mv(filename, filename + ".temp")

# Create "file" file
File.open(filename, "w") do |out|

# Write in "file" the modified "file.temp" lines.
IO.foreach(filename + ".temp") do |line|

if line =3D~ /FECHAIMPRE/
line.gsub!(/FECHAIMPRE/, $fecha)
end

out << line
end

end

# Erase the temporal
FileUtils.rm(filename + ".temp", :force =3D> true)
end

Dir.glob("*.tmp").each { |file| UpdateDates(file) }

#EOF#################################################################

But i keep thinking there has to be a shorter way. I read the ruby
documentation
for IO and File classes but i haven't found any better solution.

What do you think about it???

Thanks!

This should work (beware, it's untested).

fecha=3D Time.now.strftime("%d/%m/%Y")

Dir.glob("*.tmp").each do |filename|
text=3DFile.read filename
File.open(filename,'w'){|f| f.write text.gsub(/FECHAIMPRE/,$fecha)}
end

Stefano
 
E

Emmanuel Oga

Stefano:
your way worked very well, thank you!

Robert:
I have prolems with your code, i think is because of the way my
bourne shell is interpreting the comand line. Beside, i prefer to code
the solution
inside my current script (it does another things). I don't want to quote
your code in backticks because i think that would spawn two ruby
interpreters
for this simple task.

Thank for you both!
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top