146486Sbostic# 2*62233Sbostic# @(#)math.sed 8.1 (Berkeley) 06/06/93 346486Sbostic# 446486Sbostic# Addition and multiplication in sed. 546486Sbostic# ++ for a limited time only do (expr) too!!! 646486Sbostic# 746486Sbostic# Kevin S Braunsdorf, PUCC UNIX Group, ksb@cc.purdue.edu. 846486Sbostic# 946486Sbostic# Ex: 1046486Sbostic# echo "4+7*3" | sed -f %f 1146486Sbostic 1246486Sbostic# make sure the expression is well formed 1346486Sbostics/[ ]//g 1446486Sbostic/[+*\/-]$/{ 1546486Sbostic a\ 1646486Sbostic poorly formed expression, operator on the end 1746486Sbostic q 1846486Sbostic} 1946486Sbostic/^[+*\/]/{ 2046486Sbostic a\ 2146486Sbostic poorly formed expression, leading operator 2246486Sbostic q 2346486Sbostic} 2446486Sbostic 2546486Sbostic# fill hold space with done token 2646486Sbosticx 2746486Sbostics/^.*/done/ 2846486Sbosticx 2946486Sbostic 3046486Sbostic# main loop, process operators (*, + and () ) 3146486Sbostic: loop 3246486Sbostic/^\+/{ 3346486Sbostic s/// 3446486Sbostic b loop 3546486Sbostic} 3646486Sbostic/^\(.*\)(\([^)]*\))\(.*\)$/{ 3746486Sbostic H 3846486Sbostic s//\2/ 3946486Sbostic x 4046486Sbostic s/^\(.*\)\n\(.*\)(\([^()]*\))\(.*\)$/()\2@\4@\1/ 4146486Sbostic x 4246486Sbostic b loop 4346486Sbostic} 4446486Sbostic/^[0-9]*\*/b mul 4546486Sbostic/^\([0-9]*\)\+\([0-9+*]*\*[0-9]*\)$/{ 4646486Sbostic s//\2+\1/ 4746486Sbostic b loop 4846486Sbostic} 4946486Sbostic/^[0-9]*\+/{ 5046486Sbostic s/$/=/ 5146486Sbostic b add 5246486Sbostic} 5346486Sbosticx 5446486Sbostic/^done$/{ 5546486Sbostic x 5646486Sbostic p 5746486Sbostic d 5846486Sbostic} 5946486Sbostic/^()/{ 6046486Sbostic s/// 6146486Sbostic x 6246486Sbostic G 6346486Sbostic s/\(.*\)\n\([^@]*\)@\([^@]*\)@\(.*\)/\2\1\3/ 6446486Sbostic x 6546486Sbostic s/[^@]*@[^@]*@\(.*\)/\1/ 6646486Sbostic x 6746486Sbostic b loop 6846486Sbostic} 6946486Sbostici\ 7046486Sbostichelp, stack problem 7146486Sbosticp 7246486Sbosticx 7346486Sbosticp 7446486Sbosticq 7546486Sbostic 7646486Sbostic# turn mul into add until 1*x -> x 7746486Sbostic: mul 7846486Sbostic/^0*1\*/{ 7946486Sbostic s/// 8046486Sbostic b loop 8146486Sbostic} 8246486Sbostic/^\([0-9]*\)0\*/{ 8346486Sbostic s/^\([0-9]*\)0\*\([0-9]*\)/\1*\20/ 8446486Sbostic b mul 8546486Sbostic} 8646486Sbostics/^\([0-9]*\)1\*/\10*/ 8746486Sbostics/^\([0-9]*\)2\*/\11*/ 8846486Sbostics/^\([0-9]*\)3\*/\12*/ 8946486Sbostics/^\([0-9]*\)4\*/\13*/ 9046486Sbostics/^\([0-9]*\)5\*/\14*/ 9146486Sbostics/^\([0-9]*\)6\*/\15*/ 9246486Sbostics/^\([0-9]*\)7\*/\16*/ 9346486Sbostics/^\([0-9]*\)8\*/\17*/ 9446486Sbostics/^\([0-9]*\)9\*/\18*/ 9546486Sbostics/\*\([0-9*]*\)/*\1+\1/ 9646486Sbosticb mul 9746486Sbostic 9846486Sbostic# get rid of a plus term until 0+x -> x 9946486Sbostic: add 10046486Sbostic/^\+\([0-9+*]*\)=/{ 10146486Sbostic s//\1/ 10246486Sbostic b loop 10346486Sbostic} 10446486Sbostic/^\([0-9*]*\)\+=/{ 10546486Sbostic s//\1/ 10646486Sbostic b loop 10746486Sbostic} 10846486Sbostic/^\([0-9]*\)\+\([0-9*+]*\)\+=/{ 10946486Sbostic s//\2+\1/ 11046486Sbostic b loop 11146486Sbostic} 11246486Sbostic/^\([0-9]*\)0\+\([0-9]*\)\([0-9]\)=/{ 11346486Sbostic s//\1+\2=\3/ 11446486Sbostic b add 11546486Sbostic} 11646486Sbostic/^\([0-9]*\)\([0-9]\)\+\([0-9]*\)0=/{ 11746486Sbostic s//\1+\3=\2/ 11846486Sbostic b add 11946486Sbostic} 12046486Sbostic/^\([0-9]*\)0\+\([0-9*+]*\)\+\([0-9]*\)\([0-9]\)=/{ 12146486Sbostic s//\1+\2+\3=\4/ 12246486Sbostic b add 12346486Sbostic} 12446486Sbostic/^\([0-9]*\)\([0-9]\)\+\([0-9*+]*\)\+\([0-9]*\)0=/{ 12546486Sbostic s//\1+\3+\4=\2/ 12646486Sbostic b add 12746486Sbostic} 12846486Sbostics/^\([0-9]*\)1\+/\10+/ 12946486Sbostics/^\([0-9]*\)2\+/\11+/ 13046486Sbostics/^\([0-9]*\)3\+/\12+/ 13146486Sbostics/^\([0-9]*\)4\+/\13+/ 13246486Sbostics/^\([0-9]*\)5\+/\14+/ 13346486Sbostics/^\([0-9]*\)6\+/\15+/ 13446486Sbostics/^\([0-9]*\)7\+/\16+/ 13546486Sbostics/^\([0-9]*\)8\+/\17+/ 13646486Sbostics/^\([0-9]*\)9\+/\18+/ 13746486Sbostic 13846486Sbostics/9=\([0-9]*\)$/_=\1/ 13946486Sbostics/8=\([0-9]*\)$/9=\1/ 14046486Sbostics/7=\([0-9]*\)$/8=\1/ 14146486Sbostics/6=\([0-9]*\)$/7=\1/ 14246486Sbostics/5=\([0-9]*\)$/6=\1/ 14346486Sbostics/4=\([0-9]*\)$/5=\1/ 14446486Sbostics/3=\([0-9]*\)$/4=\1/ 14546486Sbostics/2=\([0-9]*\)$/3=\1/ 14646486Sbostics/1=\([0-9]*\)$/2=\1/ 14746486Sbostic/_/{ 14846486Sbostic s//_0/ 14946486Sbostic : inc 15046486Sbostic s/9_/_0/ 15146486Sbostic s/8_/9/ 15246486Sbostic s/7_/8/ 15346486Sbostic s/6_/7/ 15446486Sbostic s/5_/6/ 15546486Sbostic s/4_/5/ 15646486Sbostic s/3_/4/ 15746486Sbostic s/2_/3/ 15846486Sbostic s/1_/2/ 15946486Sbostic s/0_/1/ 16046486Sbostic s/\+_/+1/ 16146486Sbostic /_/b inc 16246486Sbostic} 16346486Sbosticb add 164