This is to answer to my own question, which completely different compare to my original question, but it serves exactly (well, more or less) the same purpose. So, as Sergio suggested: Say the example input file is something like this:
maci:ruby san$ cat OSs.txt
Slackware, Linux, i-num=1
Jaguar, MacX, i-num=6
Chicago, this_Win, i-num=2
Daytona, an_other_Win, i-num=7
RedHat, Linux, i-num=5
Lion, MacY, i-num=4
Caldera, Linux, i-num=9
Longhorn, that_Win, i-num=8
Tiger, MacZ, i-num=3
Indiana, Solaris, i-num=10
Kodiak, MacX, i-num=11
The actual file is dynamically created with variable number of OSs i.e. the file may or may not have Mac or Win at all. Taking from there, this is what I came up so far....
inFile = "OSs.txt"
os = {}
open(inFile, 'r').each do |line|
next if line =~ /^\s*(#|$)/
if line.split(',').map(&:strip)[1] =~ /^Mac/
(os[:Mac] ||= []) << line.split(',').map(&:strip)[0]
end
if line.split(',').map(&:strip)[1] =~ /_Win$/
(os[:Win] ||= []) << line.split(',').map(&:strip)[0]
end
if line.split(',').map(&:strip)[1] !~ /(^Mac|_Win$)/
(os[:Linux] ||= []) << line.split(',').map(&:strip)[0]
end
end
os.each_key do |cls|
p "%s [%s]" % [os[cls][0], os[cls].count]
end
It's pretty much doing what I actually want but I believe there are a lot better ways of doing it. Cheers!!