■行番号を付ける

■書式

txt = fh.gets
count = count + 1
fw.print "#{count}:#{txt}"

■説明

行番号を付けるにはファイル内容をgetsで1行ずつ読み込み、変数の値を1ずつ増やします。この変数の値とgetsで読み込まれたテキストを新たなファイルに書き出します。これをファイルの終わりまで繰り返します。

■サンプル

#!/usr/bin/ruby -Ku
fh = open("sample.txt","r")
fw = open("result.txt","w")
count = 0
while !fh.eof
txt = fh.gets
count = count + 1
fw.print "#{count}:#{txt}"
end
fw.close
fh.close