# Some file operations

**URL:** <https://www.ruby-forum.com/t/some-file-operations/115644>\
**Category:** Ruby\
**Created:** [September 27, 2007, 5:24am UTC](https://www.ruby-forum.com/t/some-file-operations/115644 "2007-09-27T05:24:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [September 27, 2007, 5:24am UTC](https://www.ruby-forum.com/t/some-file-operations/115644/1 "2007-09-27T05:24:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [September 27, 2007, 6:32am UTC](https://www.ruby-forum.com/t/some-file-operations/115644/2 "2007-09-27T06:32:00Z")

</div>

On 27 Sep 2007, at 12:23, blufur wrote:

> 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 G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [September 27, 2007, 6:40am UTC](https://www.ruby-forum.com/t/some-file-operations/115644/3 "2007-09-27T06:40:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [September 27, 2007, 7:14am UTC](https://www.ruby-forum.com/t/some-file-operations/115644/4 "2007-09-27T07:14:47Z")

</div>

blufur wrote:

> 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

---

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [September 27, 2007, 9:12am UTC](https://www.ruby-forum.com/t/some-file-operations/115644/5 "2007-09-27T09:12:42Z")

</div>

blufur wrote:

> A couple quick questions:
> 
> how can my ruby program tell me how many lines are in a text file?  
> If it’s huge, `wc -l` is probably best.

---

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [September 27, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/some-file-operations/115644/6 "2007-09-27T13:27:14Z")

</div>

7stud – wrote:

> 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()

---

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [October 5, 2007, 8:03am UTC](https://www.ruby-forum.com/t/some-file-operations/115644/7 "2007-10-05T08:03:21Z")

</div>

On 9/26/07, blufur [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Maybe a unix command that could be run by the ruby script that magically  
> gives you last line of a file

I am relatively inexperienced in UNIX but the command  
tail -1 /path/to/file.ext  
should return the last line of any file. I don’t know if there’s any way  
to  
let you write to it.

---

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [October 5, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/some-file-operations/115644/8 "2007-10-05T13:04:59Z")

</div>

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

In UNIX:

v = `wc -l /etc/passwd`  
puts v

Victor

---

<div class="post-metadata">

**Author:** ![blufur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@blufur](https://www.ruby-forum.com/u/blufur)\
**Post date:** [September 27, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/some-file-operations/115644/9 "2007-09-27T14:57:01Z")

</div>

On Sep 26, 2007, at 10:23 PM, blufur wrote:

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

Probably the easiest code for this is:

line\_count = 0  
File.foreach(…) { line\_count = $. }

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

line\_count = 0  
File.open(ARGV.shift) do |f|  
while block = f.read(1024)  
line\_count += 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  
> (not add another line at the end, but get and then change the line  
> at the end).

Firefly:~/Desktop$ cat edit\_last\_line.rb  
#!/usr/bin/env ruby -wKU

require “rubygems”  
require “elif”

file = ARGV.shift

# read the last line

last\_line = 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/,  
“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 G. II
