用可变数量的空格替换制表符,保持对齐 [英] Replace tab with variable amount of spaces, maintaining the alignment
问题描述
我有一个制表符分隔的文件,由7列组成.
I have a tab separated file, consisting of 7 columns.
ABC 1437 1 0 71 15.7 174.4
DEF 0 0 0 1 45.9 45.9
GHIJ 2 3 0 9 1.1 1.6
我需要用可变数量的空格字符替换制表符,以便保持列对齐.请注意,我不不希望每个标签都被8个空格代替.相反,我希望在第1行第1列(8-length(ABC)= 5)之后有5个空格,在第1行第2列(8-length(1437)= 4)之后等等,等等.
What I need is to replace the tab character with variable amount of space characters in order ot maintain the column alignment. Note that, I do not want every tab to be replaced by 8 spaces. Instead, I want 5 spaces after row #1 column #1 (8 - length(ABC) = 5), 4 spaces after row #1 column #2 (8 - length(1437) = 4), etc.
是否有一个Linux工具可以为我做,还是我应该自己编写?
Is there a linux tool to do it for me, or I should write it myself?
推荐答案
POSIX实用程序 pr 称为 pr -e -t
完全满足您的要求,并且在每个Unix安装中都存在AFAIK.
The POSIX utility pr called as pr -e -t
does exactly what you want and AFAIK is present in every Unix installation.
$ cat file
ABC 1437 1 0 71 15.7 174.4
DEF 0 0 0 1 45.9 45.9
GHIJ 2 3 0 9 1.1 1.6
$ pr -e -t file
ABC 1437 1 0 71 15.7 174.4
DEF 0 0 0 1 45.9 45.9
GHIJ 2 3 0 9 1.1 1.6
,并且带有显示为 ^ I
s的标签:
and with the tabs visible as ^I
s:
$ cat -ET file
ABC^I1437^I1^I0^I71^I15.7^I174.4$
DEF^I0^I0^I0^I1^I45.9^I45.9$
GHIJ^I2^I3^I0^I9^I1.1^I1.6$
$ pr -e -t file | cat -ET
ABC 1437 1 0 71 15.7 174.4$
DEF 0 0 0 1 45.9 45.9$
GHIJ 2 3 0 9 1.1 1.6$
这篇关于用可变数量的空格替换制表符,保持对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!