How to converts tabs to spaces in the Linux system?


While working with files, sometimes a situation occurs that a file contains many tabs and the requirement is a file in which no tabs but with space. In this case, if a file is quite simple then work is easy but if we are dealing with a long file then it’s very tough work.

To convert tabs to spaces, we use the expand command in the Linux system. If file is not given then the expand command read standard input.

Syntax

The general syntax of the expand command as follows −

expand [OPTION]... [FILE]...

Brief description of options available in the expand command.

Sr.No.Option & Description
1-i,  --initial
Don’t convert tabs after non blanks
2-t,  --tabs=N
Have tabs N characters apart, not eight
3-t,  --tabs=LIST
Use commas separated list of tab positions
4--help
Displays a help message and then exits.
5--version
It gives info about the version and then exits.

To converts each tab into specified space in the Linux system, we use the -t or --tab option with the expand command.

First, we need to create a file using the cat command in which tabs contain.

$ cat >text.txt
Hey,   welcome   to    tutorialspoint...
^C

Then after to converts these tabs into space, we use the expand command as shown below.

$ expand –-tabs=1 text.txt
Hey, welcome to tutorialspoint...

We have already seen the above example to converts tabs into spaces from files. To converts tabs into space from standard input, we use the expand command as shown below.

$ expand –-tabs=1
Hey,    welcome    to    tutorialspoint...
Hey, welcome to tutorialspoint...

To stop conversion after non blanks, we use the -i or --initial option with the expand command as shown below.

$ expand -i text.txt

Or

$ expand --initial text.txt

To check more information about the expand command, we use the --help option with the expand command as shown below.

$ expand --help

To check version information of the expand command, we use the --version option with the expand command as shown below.

$ expand --version

After executing the above command, in which version expand command is working is prompt.

Updated on: 30-Jun-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements