以下のスクリプトはcat -nの置換えです.実際それは,出力を
GNU catのように正確に書式化します.
もちろん,これは二つの理由から全く意味がありません.まず始めに他のもの はCで行ないます.二番目に以下のBourneシェルスクリプトは同じ目的で使用さ れ,はるかに速くなります.
#! /bin/sh
sed -e "=" $@ | sed -e '
s/^/ /
N
s/^ *\(......\)\n/\1 /
'
それは行番号を出力するためにsedを使用し,二つのNで行を
二つにまとめます.もちろん,このスクリプトは以下で提示するものほど教わ
るものはありません.
増加で使用しているアルゴリズムを両方のバッファで使用しているので,行は
可能な限り速く出力され,そして破棄されます.数値は変更した桁がバッファ
に入り,変更されないものがもう一方に行くように分離されています.変更さ
れる桁は単一のステップ(yコマンドを使用して)修正されます.次の行
の行番号は,次の繰り返しで使用されるように,作成されホールド空間に保存
されます.
#!/usr/bin/sed -nf
# Prime the pump on the first line
x
/^$/ s/^.*$/1/
# Add the correct line number before the pattern
G
h
# Format it and print it
s/^/ /
s/^ *\(......\)\n/\1 /p
# Get the line number from hold space; add a zero
# if we're going to add a digit on the next line
g
s/\n.*$//
/^9*$/ s/^/0/
# separate changing/unchanged digits with an x
s/.9*$/x&/
# keep changing digits in hold space
h
s/^.*x//
y/0123456789/1234567890/
x
# keep unchanged digits in pattern space
s/x.*$//
# compose the new number, remove the newline implicitly added by G
G
s/\n//
h