unexpand command in Linux with Examples

To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command.

The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here’s the syntax of unexpand command :
Syntax :

$unexpand [OPTION]... [FILE]...

where, OPTION refers to the options compatible with unexpand and FILE refers to the fie name.



Using unexpand command

To convert all the space characters into tab characters in the file kt.txt , use unexpand as :

$cat -vet kt.txt
have    a    nice    day$
always    try    harder$
to    achieve    better$


/* In the below output $ refers to 
   the new line feed and ^I refers 
   to the tab */
$unexpand kt.txt
have^Ia^Inice^Iday$
always^Itry^Iharder$
to^Iachieve^Ibetter$

In order to save the produced output by unexpand command in another file let’s say dv.txt use the below syntax :

/* Saving output in file, dv.txt */
$unexpand kt.txt>dv.txt

$cat -vet dv.txt
have^Ia^Inice^Iday$
always^Itry^Iharder$
to^Iachieve^Ibetter$
 

Options for unexpand command

  • -a, – -all option : This option is used to convert all blanks, instead of just initial blanks (which is by default).
    /* This converts all the blanks 
       also into tabs */
    $unexpand -a kt.txt>dv.txt
    
  • – -first-only option : This is used to convert only leading sequences of blanks (overrides -a option).
    /* This converts only the leading
       sequences of blanks */
    $unexpand --first-only kt.txt>dv.txt
    
  • -t, – -tabs=N option : This set tabs N characters apart instead of the default of 8 (enables -a option).
    /* the -t option with numerical value 
       2 forces to change the spaces
       into tabs of only 2 characters */
    $unexpand -t2 kt.txt>dv.txt
    
  • -t, – -tabs=LIST option : This option uses comma separated LIST of tab positions (enables -a option).
  • – -help option : This display a help message and exit.
  • – -version option : This display version information and exit.

Also see : expand command



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.