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.
on 2013-02-13 23:30
on 2013-02-13 23:48
Sloan Ruby 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.
on 2013-02-14 00:08
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 2013-02-14 00:56
On Wed, Feb 13, 2013 at 3:08 PM, Sloan Ruby <lists@ruby-forum.com> 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 :-) 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,
on 2013-02-14 06:39
On Wed, Feb 13, 2013 at 4:30 PM, Sloan Ruby <lists@ruby-forum.com> 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 2013-02-14 18:17
On Thu, Feb 14, 2013 at 12:49 AM, Hassan Schroeder <hassan.schroeder@gmail.com> wrote: >> } >> end > > You've commented out the contentsArray.push line which insures > that it's always empty, for one thing :-) 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
on 2013-02-14 18:32
Robert Klemme 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 2013-02-14 19:29
On Feb 14, 2013, at 12:32 PM, Joel Pearson wrote: > Robert Klemme 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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.