博客
关于我
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命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基本知识点梳理和查询优化
查看>>
mysql基础
查看>>
Mysql基础 —— 数据基础操作
查看>>