Linux常用命令-expand

命令

expand

描述

convert tabs to spaces
转换tab为空格

用法

1
expand [OPTION]... [FILE]...

选项

1
2
3
4
5
Options:
-i, --initial 不转换非空白字符后的tab,即只转换行首的缩进
-t, --tabs=NUMBER 设置一个tab转换为几个空格,默认为8个

如果FILE为-,则读取标准输入

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ echo -e "a\tb\tc\td"
a b c d
# 转换tab为显示4个空格
$ echo -e "a\tb\tc\td" |expand -t4
a b c d
$ echo -e "a\tb\tc\td" > tab
$ cat tab
a b c d
# 使用cat -t命令可以显示tab符号^I
$ cat -t tab
a^Ib^Ic^Id
# 转换tab为1个空格
$ expand -t1 tab
a b c d
# tab已转换为4个空格
$ expand -t4 tab > z
$ cat -t z
a b c d
$ cat -t a
# 如下文件包括行首tab和中间tab
for i in a;do
^Isodf
^Iaef
^I^Iaef
^Iaa^Ibb^Icc
done
# -i 转换行首tab
$ expand -it4 a |cat -t
for i in a;do
sodf
aef
aef
aa^Ibb^Icc
done
# 转换所有tab
$ expand -t4 a |cat -t
for i in a;do
sodf
aef
aef
aa bb cc
done