テキストを行番号付きに加工して出力するスクリプト
いや、ほんの小物です。ちょっと必要だったので自分で書きました。
(動作確認: Groovy Version: 1.5.7 JVM: 1.6.0_10)
RowNumbering.groovy
def file = new File(args[0]) def lines = file.readLines() def digit = lines.size().toString().size() println "${file.name}" println '-'*79 lines.eachWithIndex { it, idx -> println "${(idx+1).toString().padLeft(digit)}: ${it}" } println '[EOF]' println '-'*79
実行結果 (自分自身を出力)
>groovy RowNumbering.groovy RowNumbering.groovy
RowNumbering.groovy
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
1: def file = new File(args[0])
2: def lines = file.readLines()
3: def digit = lines.size().toString().size()
4: println "${file.name}"
5: println '-'*79
6: lines.eachWithIndex { it, idx ->
7: println "${(idx+1).toString().padLeft(digit)}: ${it}"
8: }
9: println '[EOF]'
10: println '-'*79
[EOF]
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
便利(?)な使い方
リダイレクト先を > RowNumbering.txt とかに変更すれば便利かも。
>groovy RowNumbering.groovy RowNumbering.groovy > RowNumbering.txt