tsv2html.sh

#!/bin/sh
set -f
echo '<table>'
nl='
'
tab='	'
bel="$(printf '\a')"
tac | sed -e "s/\t/$(printf '\a')\t/g" | (while read -r line; do
    if ([ "$line" = "" ]); then
      continue
    fi
    x=1
    printf '<tr>'
    IFS="$tab"
    for token in $(printf '%s' "$line"); do
      token="$(echo "$token" | sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g')"
      rowspan=$(eval echo -n '$'{rowspan$x:-1})
      if ([ "$rowspan" = "$nl" ]) then
        x=1
      fi
      if ([ "$token" = "$bel" ]) then
        eval rowspan$x=$(expr $rowspan + 1)
      else
        if ([ "$rowspan" = "1" ]) then
          printf '<td>%s' "$token"
        else
          printf '<td rowspan="%s">%s' "$rowspan" "$token"
        fi
        eval $(eval echo -n rowspan$x'='1)
      fi
      x=$(expr $x + 1)
    done
    printf '\n'
  done
) | tac | tr -d "$bel"
echo '</table>'