some file operations

B

blufur

A couple quick questions:

how can my ruby program tell me how many lines are in a text file?

how can i have my ruby program edit the last line of a text file (not
add another line at the end, but get and then change the line at the
end).

Thanks for any suggestions!
 
A

Alex Gutteridge

how can my ruby program tell me how many lines are in a text file?

IO.foreach iterates over the lines in a file which you can then
count, or IO.read reads the whole thing into a string from which you
can then count the number of lines.
how can i have my ruby program edit the last line of a text file
(not add another line at the end, but get and then change the line
at the end).

Easiest (IMO) is to read the whole file, edit the last line and write
back out. E.g:

lines = IO.read('file').split
lines[-1] = 'edit'
puts lines
Thanks for any suggestions!

Alex Gutteridge

Bioinformatics Center
Kyoto University
 
B

blufur

thanks for the suggestions - it is a very big file, though (many
thousands of lines), and I have to do this many times in a loop. Any
less resource-intensive way? Maybe a unix command that could be run
by the ruby script that magically gives you last line of a file, then
allows you to delete last line of file, then i can just append new to
file?

Best,
DAN

how can my ruby program tell me how many lines are in a text file?

IO.foreach iterates over the lines in a file which you can then
count, or IO.read reads the whole thing into a string from which
you can then count the number of lines.
how can i have my ruby program edit the last line of a text file
(not add another line at the end, but get and then change the line
at the end).

Easiest (IMO) is to read the whole file, edit the last line and
write back out. E.g:

lines = IO.read('file').split
lines[-1] = 'edit'
puts lines
Thanks for any suggestions!

Alex Gutteridge

Bioinformatics Center
Kyoto University
 
7

7stud --

blufur said:
A couple quick questions:

how can my ruby program tell me how many lines are in a text file?

how can i have my ruby program edit the last line of a text file (not
add another line at the end, but get and then change the line at the
end).


#Read the file and count the lines:

File.open("data.txt", "w") do |file|
(1..25).each {|num| file.puts("line #{num}")}
end

count = 0
File.open("data.txt") do |file|
file.each {count += 1}
end

puts count #25


#To change the last line of a file that isn't
#extremely large:

lines_arr = IO.readlines("data.txt")
lines_arr[-1] = "hello world\n"

File.open("temp.txt", "w") do |file|
lines_arr.each do |line|
file.write(line)
end
end

File.delete("data.txt")
File.rename("temp.txt", "data.txt")


#Display the result:

File.open("data.txt") do |file|
file.each do |line|
print line
end
end

--output--
line 1
line 2
line 3
...
...
line 23
line 24
hello world



For extremely large files on the order of 1-2GB, you can try something
like this:

last_line = nil
temp_file = File.new("temp.txt", "w")

File.open("data.txt") do |file|
file.each do |line|
if last_line
temp_file.write(last_line)
end

last_line = line
end
end

last_line = "hello world\n"
temp_file.write(last_line)
temp_file.close()

#Delete and rename as above
 
7

7stud --

7stud said:
For extremely large files on the order of 1-2GB, you can try something
like this:

last_line = nil
temp_file = File.new("temp.txt", "w")

File.open("data.txt") do |file|
file.each do |line|
if last_line
temp_file.write(last_line)
end

last_line = line
end
end

last_line = "hello world\n"
temp_file.write(last_line)
temp_file.close()

#Delete and rename as above


A change of variable names would make it clearer what that code is
doing:

previous_line = nil
temp_file = File.new("temp.txt", "w")

File.open("data.txt") do |file|
file.each do |line|
if previous_line
temp_file.write(previous_line)
end

previous_line = line
end
end

previous_line = "hello world\n"
temp_file.write(previous_line)
temp_file.close()
 
J

James Edward Gray II

how can my ruby program tell me how many lines are in a text file?

Probably the easiest code for this is:

line_count =3D 0
File.foreach(=85) { line_count =3D $. }

That's not too great performance-wise on huge files though. We can =20
optimize the reads to speed it up a little:

line_count =3D 0
File.open(ARGV.shift) do |f|
while block =3D f.read(1024)
line_count +=3D block.count("\n")
end
end

That version is more than twice as fast on the 96 MB file I tried it on.
how can i have my ruby program edit the last line of a text file =20
(not add another line at the end, but get and then change the line =20
at the end).

Firefly:~/Desktop$ cat edit_last_line.rb
#!/usr/bin/env ruby -wKU

require "rubygems"
require "elif"

file =3D ARGV.shift

# read the last line
last_line =3D Elif.open(file) { |f| f.gets }

# remove it from the file
File.truncate(file, File.size(file) - last_line.size)

# replace the line
File.open(file, "a") { |f| f.puts last_line.sub(/\blazy\b/, =20
"sleeping") }

__END__
Firefly:~/Desktop$ cat data.txt
The quick brown fox
jumped over
the lazy dog.
Firefly:~/Desktop$ ruby edit_last_line.rb data.txt
Firefly:~/Desktop$ cat data.txt
The quick brown fox
jumped over
the sleeping dog.

Hope that helps.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,337
Members
48,014
Latest member
saradhi

Latest Threads

Top