对齐文本文件中的列

对齐文本文件中的列

我在 Unix 中有一个如下文件(test.txt):

20130912-02:29:03 BT_TBL CISCUST BT_TBL_CISCUST_003015.dat
20130912-05:00:04 BT_TBL TMVLP BT_TBL_TMVLP_001026.dat
20130912-08:26:20 D_SNODE CPR8S D_SNODE_CPR8S_001772.dat
20130912-12:15:37 BT_PRA EWSDCAI BT_PRA_EWSDCAI_193228.dat
20130912-12:15:03 AMSD TTFILE AMSD_TTFILE_285130.dat
20130912-12:10:02 SIN2TTFILE SIN2_TTFILE_131635.dat
20130912-12:15:16 BT_ONEI NOAS_AM BT_ONEI_NOAS_AM_032902.dat

我需要列具有固定长度,就像这样。它们应该用制表符分隔:

20130912-02:29:03   BT_TBL    CISCUST   BT_TBL_CISCUST_003015.dat
20130912-02:30:05   BT_TBL    CISTERM   BT_TBL_CISTERM_003034.dat
20130912-05:00:04   BT_TBL    CTVLP     BT_TBL_CTVLP_001005.dat
20130912-05:00:04   BT_TBL    TMVLP     BT_TBL_TMVLP_001026.dat
20130912-08:26:20   D_SNODE   CPR8S     D_SNODE_CPR8S_001772.dat
20130912-08:20:07   D_SNODE   CPR9S     D_SNODE_CPR9S_001692.dat
20130912-08:20:08   D_SNODE   SUB9S     D_SNODE_SUB9S_001691.dat
20130912-12:15:37   BT_PRA    EWSDCAI   BT_PRA_EWSDCAI_193228.dat
20130912-12:15:03   AMSD      TTFILE    AMSD_TTFILE_285130.dat
20130912-12:20:02   DLLS      TTFILE    DLLS_TTFILE_137361.dat
20130912-12:20:02   LOGS      TTFILE    LOGS_TTFILE_150352.dat
20130912-12:10:02   SIN2      TTFILE    SIN2_TTFILE_131635.dat
20130912-12:15:16   BT_ONEI   NOAS_AM   BT_ONEI_NOAS_AM_032902.dat
20130912-12:10:25   BT_ONEI   NOAS_FF   BT_ONEI_NOAS_FF_030531.dat
20130912-12:21:01   BT_IN     AMSSC11   BT_IN_AMSSC11_565012.dat
20130912-12:22:01   BT_IN     AMSSC12   BT_IN_AMSSC12_571777.dat
20130912-12:21:32   BT_IN     FFTSC11   BT_IN_FFTSC11_580199.dat
20130912-12:22:31   BT_IN     FFTSC12   BT_IN_FFTSC12_576488.dat
20130912-12:22:02   AMSD      VP_IN     AMSD_VP_IN_770412.dat
20130912-12:24:01   DLLS      VP_IN     DLLS_VP_IN_412252.dat
20130912-12:23:02   LOGS      VP_IN     LOGS_VP_IN_440553.dat
20130912-12:20:03   SIN2      VP_IN     SIN2_VP_IN_395060.dat

我怎样才能做到这一点?

答案1

使用 GNU sed,全局将所有空格替换为制表符:

sed 's/ /\t/g' test.txt > new.txt

既然你提到了 Unix,那么你可能对 BSD 感兴趣sed,因为它不理解\t。在这里输入:

sed 's/ /Ctrl-VTab/g' test.txt > new.txt

按下Ctrl-V将获取下一个按下的键并将其逐字插入。

答案2

在文本编辑器中,使用查找和替换工具将空格替换为制表符。

相关内容