博客
关于我
expand 与 unexpand 命令实例教程 | Linux 中国
阅读量:304 次
发布时间:2019-03-03

本文共 1996 字,大约阅读时间需要 6 分钟。

640?wx_fmt=pngexpand 和 unexpand 命令用于将文件中的 TAB 字符替换为空格,反之亦然。-- Sk

本指南通过实际的例子解释两个 Linux 命令,即 expand 和 unexpand。对于好奇的人,expand 和 unexpand 命令用于将文件中的 TAB 字符替换为空格,反之亦然。在 MS-DOS 中也有一个名为 expand 的命令,它用于解压压缩文件。但 Linux 的 expand 命令只是将 TAB 转换为空格。这两个命令是 GNU coreutils 包的一部分,由 David MacKenzie 编写。

为了演示,我将在本文使用名为 ostechnix.txt 的文本文件。下面给出的所有命令都在 Arch Linux 中进行测试。

expand 命令示例

与我之前提到的一样,expand 命令使用空格替换文件中的 TAB 字符。

现在,让我们将 ostechnix.txt 中的 TAB 转换为空格,并将结果写入标准输出:

 
$ expand ostechnix.txtexpand ostechnix.txt

如果你不想在标准输出中显示结果,只需将其写入另一个文件,如下所示。

 
$ expand ostechnix.txt>output.txtexpand ostechnix.txt>output.txt

我们还可以将标准输入中的 TAB 转换为空格。为此,只需运行 expand 命令而不带文件名:

 
$ expandexpand

只需输入文本并按回车键就能将 TAB 转换为空格。按 CTRL+C 退出。

如果你不想转换非空白字符后的 TAB,请使用 -i 标记,如下所示。

 
$ expand -i ostechnix.txtexpand -i ostechnix.txt

我们还可以设置每个 TAB 为指定数字的宽度,而不是 8(默认值)。

 
$ expand -t=5 ostechnix.txtexpand -t=5 ostechnix.txt

我们甚至可以使用逗号分隔指定多个 TAB 位置,如下所示。

 
$ expand -t 5,10,15 ostechnix.txtexpand -t 5,10,15 ostechnix.txt

或者,

 
$ expand -t "5 10 15" ostechnix.txtexpand -t "5 10 15" ostechnix.txt

有关更多详细信息,请参阅手册页。

 
$ man expandman expand

unexpand 命令示例

正如你可能已经猜到的那样,unexpand 命令将执行与 expand 命令相反的操作。即它会将空格转换为 TAB。让我向你展示一些例子,以了解如何使用 unexpand 命令。

要将文件中的空白(当然是空格)转换为 TAB 并将输出写入标准输出,请执行以下操作:

 
$ unexpand ostechnix.txtunexpand ostechnix.txt

如果要将输出写入文件而不是仅将其显示到标准输出,请使用以下命令:

 
$ unexpand ostechnix.txt>output.txtunexpand ostechnix.txt>output.txt

从标准输出读取内容,将空格转换为制表符:

 
$ unexpandunexpand

默认情况下,unexpand 命令仅转换初始的空格。如果你想转换所有空格而不是只是一行开头的空格,请使用 -a 标志:

 
$ unexpand -a ostechnix.txtunexpand -a ostechnix.txt

仅转换一行开头的空格(请注意它会覆盖 -a):

 
$ unexpand --first-only ostechnix.txtunexpand --first-only ostechnix.txt

使多少个空格替换成一个 TAB,而不是 8(会启用 -a):

 
$ unexpand -t 5 ostechnix.txtunexpand -t 5 ostechnix.txt

相似地,我们可以使用逗号分隔指定多个 TAB 的位置。

 
$ unexpand -t 5,10,15 ostechnix.txtunexpand -t 5,10,15 ostechnix.txt

或者,

 
$ unexpand -t "5 10 15" ostechnix.txtunexpand -t "5 10 15" ostechnix.txt

有关更多详细信息,请参阅手册页。

 
$ man unexpandman unexpand

在处理大量文件时,expand 和 unexpand 命令对于用空格替换不需要的 TAB 时非常有用,反之亦然。


via: 

作者: 选题: 译者: 校对:

本文由  原创编译, 荣誉推出

转载地址:http://qxol.baihongyu.com/

你可能感兴趣的文章
mysql备份
查看>>
mysql备份与恢复
查看>>
mysql备份工具xtrabackup
查看>>
mysql备份恢复出错_尝试备份/恢复mysql数据库时出错
查看>>
mysql复制内容到一张新表
查看>>
mysql复制表结构和数据
查看>>
mysql复杂查询,优质题目
查看>>
MySQL外键约束
查看>>
MySQL多表关联on和where速度对比实测谁更快
查看>>
MySQL多表左右连接查询
查看>>
mysql大批量删除(修改)The total number of locks exceeds the lock table size 错误的解决办法
查看>>
mysql如何做到存在就更新不存就插入_MySQL 索引及优化实战(二)
查看>>
mysql如何删除数据表,被关联的数据表如何删除呢
查看>>
MySQL如何实现ACID ?
查看>>
mysql如何记录数据库响应时间
查看>>
MySQL子查询
查看>>
Mysql字段、索引操作
查看>>
mysql字段的细节(查询自定义的字段[意义-行列转置];UNION ALL;case-when)
查看>>
mysql字段类型不一致导致的索引失效
查看>>
mysql字段类型介绍
查看>>