Using expand and unexpand commands
In the following tutorial, we will look at how to use the "expand" and "unexpand" commands.
Expand Command
The "expand" command is used to convert tabs in files to spaces. When no file is passed or "-" is passed, then standard input is read.
Basic Syntax of command: expand [OPTION]... [FILE]...
The following text file "file1" will be used in the examples that follow. The contents of "file1" contains tabs between each field. This can be verified by using the "cat -vet" option to display hidden characters.
$ cat file1
One Two Three Four Five six seven
One Two Three Four Five six seven
One Two Three Four Five six seven
One Two Three Four Five six seven
One Two Three Four Five six seven
One Two Three Four Five six seven
One Two Three Four Five six seven
One Two Three Four Five six seven
$ cat -vet file1
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
By using the "cat -vet" option against file1, we can see that the tabs are represented by the "I^" character. The "$" denotes a line feed. Now if we use the expand command, we can convert these tabs into spaces. We will use the expand command and create a new file called "file2".
$ expand file1 > file2
$ cat -vet file2
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
Expand Command Options:
-i, --initial
do not convert tabs after non blanks
-t, --tabs=NUMBER
have tabs NUMBER characters apart, not 8
-t, --tabs=LIST
use comma separated list of explicit tab positions
--help display this help and exit
--version
output version information and exit
Unexpand Command
The "unexpand" command is used to convert space characters (blanks) into tabs in each file.
Basic Syntax of command: unexpand [OPTION]... [FILE]...
Below is an example using the file "file2" that we created earlier. From the "cat -vet" command, we can see that there are no tabs present within file2. We will now use the "unexpand command to convert these to tabs and then create a new file called "file3".
$ cat -vet file2
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
One Two Three Four Five six seven$
$ unexpand -a file2 > file3
$ cat -vet file3
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
One^ITwo^IThree^IFour^IFive^Isix^Iseven$
Expand Command Options:
-a, --all
convert all blanks, instead of just initial blanks
--first-only
convert only leading sequences of blanks (overrides -a)
-t, --tabs=N
have tabs N characters apart instead of 8 (enables -a)
-t, --tabs=LIST
use comma separated LIST of tab positions (enables -a)
--help display this help and exit
--version
output version information and exit