Expand Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

expand is a program that converts tab characters into groups of space characters, while maintaining correct alignment. It is available in Unix operating systems and many Unix-like operating systems.

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.

~/codeFactory$ expand --help 
Usage: expand [OPTION]... [FILE]...
Convert tabs in each FILE to spaces, writing to standard output.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -i, --initial    do not convert tabs after non blanks
  -t, --tabs=N     have tabs N characters apart, not 8
  -t, --tabs=LIST  use comma separated list of tab positions
                     The last specified position can be prefixed with '/'
                     to specify a tab size to use after the last
                     explicitly specified tab stop.  Also a prefix of '+'
                     can be used to align remaining tab stops relative to
                     the last specified tab stop instead of the first column
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report expand translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/expand>
or available locally via: info '(coreutils) expand invocation'

Example:

Suppose you have a file containing tab characters. You can use expand as:

~/codeFactory$ expand test3.txt 
Code    Factory
2  spaces
3   spaces
4    spaces
2               tabs
3                       tabs

Using expand with options:

1. -i, –initial: 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.

~/codeFactory$ expand -i test3.txt

/*this will not change those tabs that appear after blanks*/

2. -t, –tabs=N: 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.

~/codeFactory$ expand -t2 test3.txt > test5 

~/codeFactory$ vi test5
Code  Factory
2  spaces
3   spaces
4    spaces
2   tabs
3     tabs

same as above command
~/codeFactory$ expand --tabs=2 test3.tx > test5

3. -t, –tabs=LIST: This uses comma separated LIST of tab positions.

Leave a comment