I have some files that are indented by tabs or spaces or both. I want to convert leading tabs to spaces (one tab to 4 spaces). This includes tabs after some leading spaces. Below are some examples of some input line and the expected result.
+-----+---------------+-----------------+ | No. | Original line | Expected result | +-----+---------------+-----------------+ | 1 | \t␣xxx | ␣␣␣␣␣xxx | | 2 | ␣␣␣\txxx | ␣␣␣␣␣␣␣xxx | | 3 | \t␣\txxx | ␣␣␣␣␣␣␣␣␣xxx | | 4 | \tx\txx | ␣␣␣␣x\txx | +-----+---------------+-----------------+
I can't use the expand command here, as it can't handle the cases when spaces and tabs are mixed. Below is an example.
user1@ubuntu$ printf "\t \txxx" | od -t a
0000000 ht sp sp ht x x x
0000007
user1@ubuntu$ printf "\t \txxx" | expand -i -t 4 | od -t a
0000000 sp sp sp sp sp sp sp sp x x x
0000013
user1@ubuntu$
As you can see, the two spaces in the original string were simply dropped. How to solve my problem? I've read other similar questions on this site, but they are not exactly the same as my problem.