Showing Line Numbers : Utilities « File Directory « Ruby

Showing Line Numbers

def get_lines(filename)
  return, 'r').readlines

def get_format(lines)
  return "%0#{lines.size.to_s.size}d"

def get_output(lines)
  format = get_format(lines)
  output = ''
  lines.each_with_index do |line,i|
    output += "#{sprintf(format, i+1)}: #{line}"
  return output

print get_output(get_lines(ARGV[0]))


Related examples in the same category

1.delete a folder recursively
2.Find files
3.Delete with condition
4.Get free disk space under unix with df command
5.Get free disk space under windows with dir command
6.Call system function to delete a folder
7.Delete folder older than a certain time
8.Parse line: value|value
9.Change a | delimited file