Two things,
sed -i is your friend - sed -i XXX.txt 's/^[ ]\{2\}/\t/g'
- You can't make regular expression to multiply the tab replacement by the space length.
Given my AWK-fu is not strong (and I don't know if it can do what #2 can't), I will write a PHP script to calculate the spaces and replace them with tabs.