On Aug 12, 3:09*pm, Toki Toki <tok...@gmail.com> wrote:
> Hi all,
>
> Is it possible to use ruby like awk, sed or cut in unix to editing
> strings?
>
> Let say I have to write INSERT query like this:
>
> This is the first (1°) item.
> This is the second (2°) item.
> This is the third (3°) item.
> ..
>
> In one file I have the word-number (first, second, third) and in another
> file I have the real number (1°, 2°, 3°) of the item, both on each line.
Here's some Ruby code that may do what you're interested in:
====
d1 = open("f1.txt") do |f|
f.readlines.map { |line| line.chomp }
end
d2 = open("f2.txt") do |f|
f.readlines.map { |line| line.chomp }
end
d3 = [d1, d2].transpose
p d1, d2, d3 # just so you can see what's happened so
far
d3.each do |str1, str2|
puts "This is the #{str1} (#{str2}) item."
end
====
That's assuming f1.txt contains "first", "second", etc., one per line
and that f2.txt contains "1st", "2nd", etc., one per line.
Eric
====
Are you looking for on-site Ruby or Ruby on Rails training
that's been highly reviewed by former students?
http://LearnRuby.com