Whenever you work with files in LINUX there can be a situation when you are stuck with a file that contains a lot of tabs in it and whatever you need to do with file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you need to do this for a large number of files. For a situation like this, LINUX has a command line utility called expand which allows you to convert tabs into spaces in a file and when no file is specified it reads from standard input.
Thus, expand is useful for pre-processing character files like before sorting that contain tab characters. expand actually writes the produced output to standard output with tab characters expanded to space characters. In this, backspace characters are preserved into the output and also decrement the column count for tab calculations.
Syntax of expand :
//...syntax of expand...// $expand [OPTION] FILE
The syntax of this is quite simple to understand. It just requires a file name FILE in which you want to expand tab characters into space characters and it reads from standard input if no file name is passed and gives result to standard output.
Suppose you have a file name kt.txt containing tab characters. You can use expand as:
//using expand// $expand kt.txt /* expand will produce the content of the file in output with only tabs changed to spaces*/
Note, if there is a need to make this type of change in multiple files then you just have to pass all file names in input and tabs will get converted into spaces.
You can also transfer the output of the changes made into some other file like:
$expand kt.txt > dv.txt /*now the output will get transfer to dv.txt as redirection operator > is used*/
Options for expand command:
1. -i, – – initial option : There can be a need to convert tabs that preceed lines and leave unchanged those that appear after non-blanks. In simple words this option allows no conversion of tabs after non-blanks.
//using -i option// $expand -i kt.txt /*this will not change those tabs that appear after blanks*/
2. -t, – – tabs=N option : By default, expand converts tabs into the corresponding number of spaces. But it is possible to tweak the number of spaces using the -t command line option. This option requires you to enter the new number of spaces(N) you want the tabs to get converted.
//using -t option// $expand -t1 kt.txt > dv.txt /*this will convert the tabs in kt.txt to 1 space instead of default 8 spaces*/
You can also use it as:
$expand --tabs=1 kt.tx > dv.txt /*this will also convert tabs to one space each*/
3. -t, – -tabs=LIST option : This uses comma separated LIST of tab positions.
4. – -help : This will display a help message and exit.
5. –version : This will display version information and exit.
The number of options is not much when it comes to expanding command. So, that’s pretty much everything about expand command.
Also see : unexpand command
- man command in Linux with Examples
- du command in Linux with examples
- df command in Linux with Examples
- atq command in linux with examples
- top command in Linux with Examples
- ed command in Linux with examples
- tty command in Linux with examples
- pwd command in Linux with Examples
- fc Command in Linux with Examples
- last command in Linux with Examples
- bc command in Linux with examples
- cc command in Linux with Examples
- help Command in Linux with examples
- dir command in Linux with examples
- atd command in Linux with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.