Thursday, January 17, 2008

HowTo: Convert Tabs In Each File To Spaces

Converting tab characters to space characters can be done quite easily in linux. This task can be handy on removing tab characters in a file and convert them to specified space characters at the same time.

If you have just exported an article or data into a file with tab-columnized or tab-separated file format, and you wish to replace tab character with space characters, this blog entry is just right for that task.

Here's an entry on how to remove tabs characters in a file and convert them to specified number of space characters ?

# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a a a a
b b b b
c c c c
123 123 123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# cat -t testfile.txt
~~~~~~~~~~~~~~~~~~~~~
a^Ia^Ia^Ia
b^Ib^Ib^Ib
c^Ic^Ic^Ic
123^I^I123^I^I123
~~~~~~~~~~~~~~~~~~~~~

Now, to convert those tab characters into single space character, simply

# expand -1 testfile.txt
Result:
~~~~~~~~~~~~~~~
a a a a
b b b b
c c c c
123 123 123
~~~~~~~~~~~~~~~

To convert tab character to 4 space characters
# expand -4 testfile.txt
Result:
~~~~~~~~~~~~~~~~~~~~~~~
a a a a
b b b b
c c c c
123 123 123
~~~~~~~~~~~~~~~~~~~~~~~

Enjoy.

Related Readings:
String Manipulation Using tr
String Manipulation Using cut
Deleting New and Line Character from a File

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy