[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

read a specific line from a file

Bu Mihai

9/7/2007 8:03:00 AM

how can i read only a line from a txt file?
for example i want to read only line 3
--
Posted via http://www.ruby-....

14 Answers

Pierre-Charles David

9/7/2007 8:25:00 AM

0

2007/9/7, Bulhac Mihai <mihai.bulhac@yahoo.com>:
> how can i read only a line from a txt file?
> for example i want to read only line 3

% cat one_line.rb
#!/usr/bin/env ruby

def read_one_line(file_name, line_number)
File.open(file_name) do |file|
current_line = 1
file.each_line do |line|
return line if line_number == current_line
current_line += 1
end
end
end

puts read_one_line(ARGV[0], ARGV[1].to_i)

% ruby one_line.rb one_line.rb 3
def read_one_line(file_name, line_number)

--
http://pc...

Peña, Botp

9/7/2007 8:29:00 AM

0

From: Bulhac Mihai [mailto:mihai.bulhac@yahoo.com]
# how can i read only a line from a txt file?
# for example i want to read only line 3

you can open the file and then read each line, collecting the lines you want. exit anytime if you have what you want.

if you want a ready-made solution (written in ruby, of course), you can use rio.

irb(main):005:0> require 'rio'
=> true

# get first 4 lines (as always in ruby indexing starts at 0)

irb(main):006:0> rio('test.txt').lines[0..3]
=> ["1testing \n", "2testing \n", "3asdfasdf\n", "\n"]

# get first lines 4 to 6

irb(main):007:0> rio('test.txt').lines[3..5] # a range of lines
=> ["\n", "4asdf\n", "[]\n"]

# thus, reading line 3 would be

irb(main):013:0> rio('test.txt').lines[2..2]
=> ["3asdfasdf\n"]

kind regards -botp

expandafter

9/7/2007 9:02:00 AM

0

On Sep 7, 3:03 am, Bulhac Mihai <mihai.bul...@yahoo.com> wrote:
> how can i read only a line from a txt file?
> for example i want to read only line 3
> --
> Posted viahttp://www.ruby-....

ruby -e "puts ARGF.to_a[2]" myfile

Another way:

File.open("myfile"){|f|
line = nil
3.times { line = f.gets }
puts line
}

If the whole file will fit in memory,
you can do this:

puts IO.readlines("myfile")[2]

Robert Klemme

9/7/2007 9:50:00 AM

0

2007/9/7, Bulhac Mihai <mihai.bulhac@yahoo.com>:
> how can i read only a line from a txt file?
> for example i want to read only line 3

sed -ne '3 p' your_file

robert

rio4ruby

9/7/2007 1:06:00 PM

0

On Sep 7, 1:28 am, Pe?a, Botp <b...@delmonte-phil.com> wrote:
> From: Bulhac Mihai [mailto:mihai.bul...@yahoo.com]
> # how can i read only a line from a txt file?
> # for example i want to read only line 3
>
> if you want a ready-made solution (written in ruby, of course), you can use rio.
>
> irb(main):005:0> require 'rio'
> => true
>
> # get first 4 lines (as always in ruby indexing starts at 0)
>
> irb(main):006:0> rio('test.txt').lines[0..3]
> => ["1testing \n", "2testing \n", "3asdfasdf\n", "\n"]
>
> # get first lines 4 to 6
>
> irb(main):007:0> rio('test.txt').lines[3..5] # a range of lines
> => ["\n", "4asdf\n", "[]\n"]
>
> # thus, reading line 3 would be
>
> irb(main):013:0> rio('test.txt').lines[2..2]
> => ["3asdfasdf\n"]
>
> kind regards -botp

Or even
rio('test.txt').line[2]



Robert Klemme

9/7/2007 1:19:00 PM

0

2007/9/7, rio4ruby <Christopher.Kleckner@gmail.com>:
> On Sep 7, 1:28 am, Peña, Botp <b...@delmonte-phil.com> wrote:
> > From: Bulhac Mihai [mailto:mihai.bul...@yahoo.com]
> > # how can i read only a line from a txt file?
> > # for example i want to read only line 3
> >
> > if you want a ready-made solution (written in ruby, of course), you can use rio.
> >
> > irb(main):005:0> require 'rio'
> > => true
> >
> > # get first 4 lines (as always in ruby indexing starts at 0)
> >
> > irb(main):006:0> rio('test.txt').lines[0..3]
> > => ["1testing \n", "2testing \n", "3asdfasdf\n", "\n"]
> >
> > # get first lines 4 to 6
> >
> > irb(main):007:0> rio('test.txt').lines[3..5] # a range of lines
> > => ["\n", "4asdf\n", "[]\n"]
> >
> > # thus, reading line 3 would be
> >
> > irb(main):013:0> rio('test.txt').lines[2..2]
> > => ["3asdfasdf\n"]
> >
> > kind regards -botp
>
> Or even
> rio('test.txt').line[2]

I know a simpler ready made solution:

ruby -ne 'puts $_ if $. == 3' your_file

But I'd really prefer the sed solution. :-)

Kind regards

robert

William James

9/7/2007 2:48:00 PM

0

On Sep 7, 4:50 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> 2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:
>
> > how can i read only a line from a txt file?
> > for example i want to read only line 3
>
> sed -ne '3 p' your_file
>
> robert

awk "3==NR" your_file

Robert Klemme

9/7/2007 3:31:00 PM

0

2007/9/7, William James <w_a_x_man@yahoo.com>:
> On Sep 7, 4:50 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> > 2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:
> >
> > > how can i read only a line from a txt file?
> > > for example i want to read only line 3
> >
> > sed -ne '3 p' your_file
> >
> > robert
>
> awk "3==NR" your_file

When I think about it, this is probably more efficient for large files
and low line numbers:

head -3 your_file | tail -1

:-)

robert

William James

9/7/2007 4:23:00 PM

0

On Sep 7, 10:30 am, "Robert Klemme" <shortcut...@googlemail.com>
wrote:
> 2007/9/7, William James <w_a_x_...@yahoo.com>:
>
> > On Sep 7, 4:50 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> > > 2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:
>
> > > > how can i read only a line from a txt file?
> > > > for example i want to read only line 3
>
> > > sed -ne '3 p' your_file
>
> > > robert
>
> > awk "3==NR" your_file
>
> When I think about it, this is probably more efficient for large files
> and low line numbers:
>
> head -3 your_file | tail -1

awk "3==NR{print;exit}" your_file

The rule is: never use anything other than awk unless
you have to.

Robert Klemme

9/7/2007 6:53:00 PM

0

On 07.09.2007 18:22, William James wrote:
> On Sep 7, 10:30 am, "Robert Klemme" <shortcut...@googlemail.com>
> wrote:
>> 2007/9/7, William James <w_a_x_...@yahoo.com>:
>>
>>> On Sep 7, 4:50 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
>>>> 2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:
>>>>> how can i read only a line from a txt file?
>>>>> for example i want to read only line 3
>>>> sed -ne '3 p' your_file
>>>> robert
>>> awk "3==NR" your_file
>> When I think about it, this is probably more efficient for large files
>> and low line numbers:
>>
>> head -3 your_file | tail -1
>
> awk "3==NR{print;exit}" your_file
>
> The rule is: never use anything other than awk unless
> you have to.

LOL

robert