This guide explains the Linux command through two practical examples, namely expand
and unexpand
. For curious people, expand
and unexpand
commands for file TAB
replacement character is a space, and vice versa. In the MS-DOS is also called a expand
command, which is used to extract the compressed file. But the Linux expand
command just TAB
converted to spaces. These two commands are part of the GNU coreutils package, written by David MacKenzie.
To demonstrate, I'll use the name in this ostechnix.txt
text file. All commands given below are tested in the Arch Linux.
expand command examples
Like I mentioned before, expand
the command to replace the file with a space TAB
character.
Now, let us be ostechnix.txt
in TAB
converted to spaces, and writes the results to standard output:
$ expand ostechnix.txt
复制代码
If you do not want to display the results on standard output, which is just another file is written, as shown below.
$ expand ostechnix.txt>output.txt
复制代码
We can also use the standard input TAB
converted to spaces. To do this, simply run the expand
command with the file name:
$ expand
复制代码
Simply enter the text and press the Enter key can be TAB
converted to spaces. Press CTRL+C
exit.
If you do not want to convert the non-white space character TAB
, use the -i
mark as shown below.
$ expand -i ostechnix.txt
复制代码
We can also set each TAB
width of the specified number, rather than 8
(the default value).
$ expand -t=5 ostechnix.txt
复制代码
We can even be separated by commas specify multiple TAB
positions, as shown below.
$ expand -t 5,10,15 ostechnix.txt
复制代码
or,
$ expand -t "5 10 15" ostechnix.txt
复制代码
For more details, please refer to the manual page.
$ man expand
复制代码
unexpand command examples
As you may have guessed, unexpand
the command will perform the expand
reverse operation command. That is, it will be converted to spaces TAB
. Let me show you some examples to learn how to use the unexpand
command.
To file a blank (of course, is a space) converted TAB
and output is written to standard output, do the following:
$ unexpand ostechnix.txt
复制代码
If you want to write the output to a file instead of to standard output only, use the following command:
$ unexpand ostechnix.txt>output.txt
复制代码
To read from standard output, converting spaces into tabs:
$ unexpand
复制代码
By default, the unexpand
command converts only initial spaces. If you want to convert all the spaces instead of only spaces at the beginning of a line, use the -a
logo:
$ unexpand -a ostechnix.txt
复制代码
Only convert spaces at the beginning of a line (please note that it will cover -a
):
$ unexpand --first-only ostechnix.txt
复制代码
So how many spaces are replaced with a TAB
, not 8
(enabled -a
):
$ unexpand -t 5 ostechnix.txt
复制代码
Similarly, we can use a comma-separated specify multiple TAB
locations.
$ unexpand -t 5,10,15 ostechnix.txt
复制代码
or,
$ unexpand -t "5 10 15" ostechnix.txt
复制代码
For more details, please refer to the manual page.
$ man unexpand
复制代码
When processing a large number of files, expand
and unexpand
commands with a space for unnecessary replacement TAB
is very useful, and vice versa.
via: www.ostechnix.com/expand-and-…
Author: SK topics: lujun9972 Translator: geekpi proofread: wxy
This article from the LCTT original compiler, Linux China is proud
Reproduced in: https: //juejin.im/post/5d079a74e51d45775b419bde