htmlファイルからコメントを取り除くには

完璧ではないが、これでなんとか実用に耐える?
うまくいかないケースとしては
下のスクリプトの場合に関していえば、
同一ラインのなかのコメントのなかに
マイナス(-)とハット(^)が共存して存在するとうまくいかない。

あと、!--なる連続した文字列がコメント内部の最初にでてくるとアウト

他にいい手ないかなぁ

hirasawa@aspire-white:~$ ./test.sh 
cat test.sh
echo --------
cat test.dat 
echo --------

cat test.dat | sed  '
s/<!-- *[^!][^-][^-][^-]* *-->//g
s/<!-- *[^!][^-][^-][^^]* *-->//g
'
--------
<td> <out1> <!--<comment> comment <comment>--> <out2> </td>
<td>out1<!-- < comment > comment < comment> -->out2</td>
<td><out1><!-- < comment > comment < comment> --><out2></td>
<td><!--<comment><comment>--> out1 <!--<comment><comment>--> out2 <!-- comment --></td>
<td><!--<comment><comment>--> out1 <!--<comment><comment>--> out2 <!-- <comment> --> out3</td>
<td><!--<comment><comment>--><out1><!--<comment><comment>--><out2></td>
<td><!--   <comment->  !  <comment>  --> !out1- <!-- !<comment><comment>--> out2</td>
<td><!--   <comment->  !  <comment>  --> <!-out1-> <!-- !<comment><comment>--> <out2> </td>
--------
<td> <out1>  <out2> </td>
<td>out1out2</td>
<td><out1><out2></td>
<td> out1  out2 </td>
<td> out1  out2  out3</td>
<td><out1><out2></td>
<td> !out1-  out2</td>
<td> <!-out1->  <out2> </td>
hirasawa@aspire-white:~$ 

ついでに、もひとつメモ。

# sed script for removing comment
s/^ *//
#  先頭部分のWhiteSpace

s/ *$//
#  おしり部分のWhiteSpace

s/^\/\*.*\*\/$//
#  ^/* + 某 + */$ を削る

s/[ ]*\/\*.*\*\/[ ]*//
#  WS + /* + 某 + */ + WSを削る

/\/\*.*/,/.*\*\//d
#  /* + 某  なる行から、某 + */ なる行まで削除


/\/\//d
#  // + 某 なる行を削除