cat -n

30分プログラム、その212。catの-nオプションつけたバージョン。要するに行番号付きのcat。
始めはzipでうまいこと行番号をつけたり、forとrangeを組合せて行番号をつけたりしたかったのだけど、うまくいかずに挫折した。なので、普通にn+=1でカウントしてる。

使い方

$ python cat-n.py hoge
  1 hoge
  2 hoge
  3 hoge
  4 hoge
  5 hoge
  6 hoge

ソースコード

#! /usr/bin/python
# -*- mode:python; coding:utf-8 -*-
#
# cat-n.py - cat with index
#
# Copyright(C) 2008 by mzp
# Author: MIZUNO Hiroki / mzpppp at gmail dot com
# http://howdyworld.org
#
# Timestamp: 2008/01/01 21:32:49
#
# This program is free software; you can redistribute it and/or
# modify it under MIT Lincence.
#

import sys
write = sys.stdout.write

io = file(sys.argv[1])
n = 1
for line in io:
    write('%3d ' % n)
    write(line)
    n += 1