Read text file

Hi,
I have one text file for example

9115 2008-06-20 07:26:35
91415 2008-06-20 07:26:32
9315 2008-06-20 07:26:33
2115 2008-06-20 07:26:05
15 2008-06-20 07:26:34
115 2008-06-20 07:26:35

i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me

Pragash Mr. wrote:

Hi,
I have one text file for example

9115 2008-06-20 07:26:35
91415 2008-06-20 07:26:32
9315 2008-06-20 07:26:33
2115 2008-06-20 07:26:05
15 2008-06-20 07:26:34
115 2008-06-20 07:26:35

i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me

Try this:

file = File.open(“name.txt”)
columns = []
file.each_line do |line|
columns << line.split(" ")[0]
end
p columns

Hi, there is another solution:

File.open(‘pp.txt’) do |infile|
while line = infile.gets #the loop ends if infile.gets == EOF
line_split = line.split(" ") #split line through spaces; return
vector
puts line_split[0] #the first element of the line is the
number
end
end

for more information of this topic:

http://pleac.sourceforge.net/pleac_ruby/fileaccess.html

Pragash Mr. wrote:

15 Â 2008-06-20 07:26:34
115 Â 2008-06-20 07:26:35

i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me

numbers = File.open(filename) do |f|
f.map do |line|
line.to_i
end
end

HTH,
Sebastian

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