[lug] Output every xth line?

jjh-blug at vieorhythms.com jjh-blug at vieorhythms.com
Sat Nov 22 13:12:24 MST 2003


>>>>> "Matt" == The Matt <thompsma at colorado.edu> writes:

Matt> Maybe I'll cast these ideas into Ruby.  I'm trying to learn that
Matt> language and I really love it.

Here's a quick ruby solution.  Starting with first_line it prints out
every nth_line after that.

enjoy,

-jeremy

[jeremyhi at rapier ruby]$ expand -t2 every_n.rb 
#!/usr/bin/env ruby

if (ARGV.size < 2) then
   puts "Usage: #{$0} first_line nth_line"
   exit 1
end

first_line = ARGV[0].to_i
nth_line = ARGV[1].to_i

cur_line = 0
cur_iter = 0
STDIN.each_line do |line|
  cur_line += 1
  if (cur_line == (first_line + (cur_iter * nth_line))) then
    print line
    cur_iter += 1
  end
end

[jeremyhi at rapier ruby]$ expand -t2 every_n.rb | ./every_n.rb 3 4
if (ARGV.size < 2) then

cur_line = 0
  if (cur_line == (first_line + (cur_iter * nth_line))) then
end

-- 
========================================================================
 Jeremy Hinegardner                              jeremy at hinegardner.org 





More information about the LUG mailing list