以下のスクリプトは,sedで算数を行なう方法を説明するものの一つ です.これは実際には可能ですが1,手動で行なうべきでしょう.
数値を一つ増加させるには,最後の桁に1を追加し,それ以降の桁を置換するだ けです.例外が一つあります.その桁の前の数値が9のとき,9が無くなるまで 増加させる必要もあります.
このBruno Haibleによる解決方法は,単一のバッファを使用しているので非常
に賢く知的です.この制限がない場合,Numbering linesで使用
されているアルゴリズムの方がより速いでしょう.それは後置される9をアンダー
スコアで置換し,複数のs
コマンドを最後の桁を増加させるために使用
し,そして,再びアンダースコアをゼロで置換することで動作します.
#!/usr/bin/sed -f
/[^0-9]/ d
# replace all leading 9s by _ (any other character except digits, could
# be used)
:d
s/9\(_*\)$/_\1/
td
# incr last digit only. The first line adds a most-significant
# digit of 1 if we have to add a digit.
#
# The tn
commands are not necessary, but make the thing
# faster
s/^\(_*\)$/1\1/; tn
s/8\(_*\)$/9\1/; tn
s/7\(_*\)$/8\1/; tn
s/6\(_*\)$/7\1/; tn
s/5\(_*\)$/6\1/; tn
s/4\(_*\)$/5\1/; tn
s/3\(_*\)$/4\1/; tn
s/2\(_*\)$/3\1/; tn
s/1\(_*\)$/2\1/; tn
s/0\(_*\)$/1\1/; tn
:n
y/_/0/