File size of each item in file list

I am trying to take the contents of a file which contains the results of
a file size search and print those out with ruby. To obtain the file
size of each line in that file.

#!/usr/bin/ruby

f = File.open("/tmp/lg_files")
contentsArray=[] # start with an empty array
f.each_line {|line|
a = contentsArray.push line
#b = File.size(a)
puts a
}

This results in each line in the file /tmp/lg_files printing.

Sloan R. wrote in post #1096778:

I am trying to take the contents of a file which contains the results of
a file size search and print those out with ruby. To obtain the file
size of each line in that file.

#!/usr/bin/ruby

f = File.open("/tmp/lg_files")
do this in a block so it closes the file afterwards:
File.read("/tmp/lg_files") do |f|
contentsArray=[] # start with an empty array
f.each_line {|line|
a = contentsArray.push line
where did “a” come from? You already have a variable storing this
information: contentsArray.
#b = File.size(a)
try “File.size contentsArray.last” instead
puts a
}

This results in each line in the file /tmp/lg_files printing.

This is what I have now. I am not able to print out each line of the
file /tmp/lg_files. There is no output. What am i missing?

#!/usr/bin/ruby

system(‘find /var -type f -size +20000 >> /tmp/lg_files’)
#do this in a block so it closes the file afterwards:
File.read("/tmp/lg_files") do |f|
contentsArray=[] # start with an empty array
f.each_line {|line|
#a = contentsArray.push line
b = File.size contentsArray.last
puts b
}
end
#system(’> /tmp/lg_files’)

On Wed, Feb 13, 2013 at 4:30 PM, Sloan R. [email protected]
wrote:

#b = File.size(a)
puts a
}

This results in each line in the file /tmp/lg_files printing.

Is this just an partial step to doing something else? Because to do
what you originally posted, all that’s required is something like
this:

IO.readlines("/tmp/lg_files").each{|l| l.chomp!; puts “#{l}
#{File.size(l)}” }

On Wed, Feb 13, 2013 at 3:08 PM, Sloan R. [email protected]
wrote:

This is what I have now. I am not able to print out each line of the
file /tmp/lg_files. There is no output. What am i missing?

File.read("/tmp/lg_files") do |f|
contentsArray=[] # start with an empty array
f.each_line {|line|
#a = contentsArray.push line
b = File.size contentsArray.last
puts b
}
end

You’ve commented out the contentsArray.push line which insures
that it’s always empty, for one thing :slight_smile:

Regardless, you don’t need that at all. Something like this:

File.open("/tmp/lg_files",“r”).readlines.each do |line|
line.chomp!;
puts “#{line} #{File.size(line)}”
end

should do what you want with fewer moving parts.

HTH,

Robert K. wrote in post #1096937:

Even worse: File.read will ignore the block. It’s simply never
executed. Otherwise one would see an error.

I didn’t notice this, it should have been
contentsArray = File.read(’/tmp/lg_files’).split $/

On Feb 14, 2013, at 12:32 PM, Joel P. wrote:

Robert K. wrote in post #1096937:

Even worse: File.read will ignore the block. It’s simply never
executed. Otherwise one would see an error.

I didn’t notice this, it should have been
contentsArray = File.read(’/tmp/lg_files’).split $/

If you want the entire set of lines, just use

contentsArray = File.readlines(’/tmp/lg_files’)

http://apidock.com/ruby/v1_9_3_125/IO/readlines/class

On Thu, Feb 14, 2013 at 12:49 AM, Hassan S.
[email protected] wrote:

}
end

You’ve commented out the contentsArray.push line which insures
that it’s always empty, for one thing :slight_smile:

Even worse: File.read will ignore the block. It’s simply never
executed. Otherwise one would see an error.

Regardless, you don’t need that at all. Something like this:

File.open("/tmp/lg_files",“r”).readlines.each do |line|

Better use

File.foreach “/tmp/lg_files” do |line|

That avoids reading the whole file into memory.

line.chomp!;
puts "#{line} #{File.size(line)}"

end

should do what you want with fewer moving parts.

I think that approach is still more complicated than necessary. First
of all, the temporary file can be omitted because with IO.popen and
similar methods one can directly read the output of the find command.

It gets even better: one can also do the file system search in Ruby.
There is no point in doing that externally. There are actually two
ways:
Find.find()
Pathname#find()

Example:

require ‘pathname’

Pathname(’/var’).find do |file|
printf “%10d %s\n”, file.size, file if file.file? && file.size >=
20_000
end

Kind regards

robert

Hah, Ruby’s always one step ahead of me :stuck_out_tongue:

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