expand and unexpand command tutorial examples

This guide explains the Linux command through two practical examples, namely expandand unexpand. For curious people, expandand unexpandcommands for file TABreplacement character is a space, and vice versa. In the MS-DOS is also called a expandcommand, which is used to extract the compressed file. But the Linux expandcommand just TABconverted 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.txttext file. All commands given below are tested in the Arch Linux.

expand command examples

Like I mentioned before, expandthe command to replace the file with a space TABcharacter.

Now, let us be ostechnix.txtin TABconverted 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 TABconverted to spaces. To do this, simply run the expandcommand with the file name:

$ expand
复制代码

Simply enter the text and press the Enter key can be TABconverted to spaces. Press CTRL+Cexit.

If you do not want to convert the non-white space character TAB, use the -imark as shown below.

$ expand -i ostechnix.txt
复制代码

We can also set each TABwidth of the specified number, rather than 8(the default value).

$ expand -t=5 ostechnix.txt
复制代码

We can even be separated by commas specify multiple TABpositions, 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, unexpandthe command will perform the expandreverse operation command. That is, it will be converted to spaces TAB. Let me show you some examples to learn how to use the unexpandcommand.

To file a blank (of course, is a space) converted TABand 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 unexpandcommand converts only initial spaces. If you want to convert all the spaces instead of only spaces at the beginning of a line, use the -alogo:

$ 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 TABlocations.

$ 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, expandand unexpandcommands with a space for unnecessary replacement TABis 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

Guess you like

Origin blog.csdn.net/weixin_33751566/article/details/93180963