将文本文件中的行与命令行对齐

时间:2015-05-29 16:20:32

标签: text command-line

我在文本文件中有数据,其中数列在几列之后重复,每个新块共享第一列行标签。我想使用命令行将行对齐到一个表中。

数据文本文件如下所示:

from operator import itemgetter
max(list,key=itemgetter(1))

我希望生成的文件看起来像:

Values      SampleA     SampleB     SampleC
Value1      1.00        2.00        3.00
Value2      3.00        2.00        1.00
Value3      2.00        1.00        3.00

Values      SampleD     SampleE     SampleF
Value1      1.00        2.00        3.00
Value2      3.00        2.00        1.00
Value3      2.00        1.00        3.00

1 个答案:

答案 0 :(得分:1)

此解决方案会创建大量临时文件,但会在之后进行清理。

# put each paragraph into it's own file:
awk -v RS= '{print > sprintf("%s_%06d", FILENAME, NR)}' data.txt 

# now, join them, and align the columns 
join data.txt_* | column -t | tee data.txt.joined

# and cleanup the temp files
rm data.txt_*

事后验证:wc -l data.txt data.txt.joined