How to Replace Whitespaces with Tabs in a File

For some reason, acquiring some unique skills and tweaks in your Linux operating system experience will positively boost your Linux administration resume and portfolio, especially for users involved in continuous projects.

One unique skill set to add to your Linux resume and portfolio is replacing whitespaces with tabs in a file.

If you are a programmer or a user that has had some exposure to various Linux-supported code editors, then you will relate to the frustrations of dealing with whitespaces.

Most coding guidelines have a preference for tabs over whitespaces. Popular code editors and IDEs are aligned to these guidelines and therefore will respond well to project files formatted with tabs over whitespace.

This tutorial will walk us through various approaches to getting rid of whitespaces in a file and replacing them with tabs.

Create Sample Reference File

A whitespace in a file can be both vertical and horizontal. In this tutorial, we will be mainly concerned with the horizontal whitespace characters.

We will create our sample file with horizontal whitespace characters which we will later try to remove through various methodologies we will cover.

$ sudo nano sample_file.txt
Create File with Whitespace Characters
Create File with Whitespace Characters

We can view this file with the cat command option --show-tabs to verify if any tabs exist within it.

$ cat --show-tabs sample_file.txt
View File with Tabs
View File with Tabs

Our file is clean of tabs, as any tabs characters would be presented with the symbol ^I as the case with the following file:

$ sudo nano output.txt 
File with Tabs
File with Tabs

You can view the above file with.

$ cat --show-tabs output.txt 
View File with Tabs
View File with Tabs

We can now go over the various approaches to removing and replacing the horizontal whitespace characters from our file with tabs.

1. Using tr Command

The tr command is effective in cases where specific characters in a file need to be deleted or translated. In our case, we will be using it to convert the horizontal whitespace characters to tabs characters and then save the result to a random output file.

$ tr " " "\t" < sample_file.txt > new_sample_file.txt

We can then use the cat command to confirm that the tabs exist in the file.

$ cat --show-tabs new_sample_file.txt 
Check Tabs in File
Check Tabs in File

Each whitespace has been replaced with a single tab character. We can replace multiple whitespaces with a single tab when we use the -s option in our command.

$ tr -s " " "\t" < sample_file.txt > new_sample_file.txt
$ cat --show-tabs new_sample_file.txt 
Replace Whitespace with Tabs
Replace Whitespace with Tabs

2. Using Awk Command

The awk programming language interpreter gained its reputation as a powerful and performant complex text processor. It is therefore an ideal alternative for converting whitespace characters to tab characters. It uses the -F option as field separator and OFS as the output field separator for the tab character.

$ awk -F'[[:blank:]]' -v OFS="\t" '{$1=$1; print}' sample_file.txt > new_sample_file.txt 

Let’s use the cat command to confirm if tabs were created.

$ cat --show-tabs new_sample_file.txt 
Replace Whitespace with Tabs Using Awk
Replace Whitespace with Tabs Using Awk

Removing the field separator -F will allow us to convert multiple whitespace characters to a single tab character.

$ awk -v OFS="\t" '{$1=$1; print}' sample_file.txt > new_sample_file.txt 
$ cat --show-tabs new_sample_file.txt 
Convert Whitespace to Single Tab
Convert Whitespace to Single Tab

3. Using sed Command

The primary role of the sed stream editor is to filter and transform text. To use it to convert whitespaces to tabs, we will implement it in the following manner.

$ sed 's/[[:blank:]]/\t/g' sample_file.txt > output.txt
$ cat --show-tabs output.txt
Convert Whitespace to Tabs Using Sed
Convert Whitespace to Tabs Using Sed

Adding an extended regular equation to the above sed command should help us convert multiple whitespace characters to single tabs characters.

$ sed 's/[[:blank:]]\+/\t/g' sample_file.txt > output.txt
$ cat --show-tabs output.txt
Convert Multiple Whitespace to Tabs
Convert Multiple Whitespace to Tabs

We have successfully learned how to replace whitespaces with tabs in a file under a Linux operating system environment.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.