Short solution for replacing Strings inside a file?

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!

On 29.01.2007 15:39, Emmanuel wrote:

   sed -e s/FECHAIMPRE/$date/g < $item > $item.po

    # Write in "file" the modified "file.temp" lines.

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

Alle 15:39, lunedì 29 gennaio 2007, Emmanuel ha scritto:

   sed -e s/FECHAIMPRE/$date/g < $item > $item.po

    # Write in "file" the modified "file.temp" lines.

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= Time.now.strftime("%d/%m/%Y")

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

Stefano

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs