Read a specific line from a file

func getNthLine(filename, n) {
  var file = File(filename)
  file.open_r.each { |line|
    Num($.) == n && return line
  }
  warn "file #{file} does not have #{n} lines, only #{$.}\n"
  return nil
}

var line = getNthLine("/etc/passwd", 7)
say line if defined(line)

Last updated