2012-08-14 89 views
0

我需要從任意數量的文件中提取第一列,並使用空行爲缺失的條目列顯示列。空行部分有問題。像這樣:使用AWK提取和填充列

File1: 
Alfa  Something More stuff 
Charlie  Something More stuff 
Delta  Something More stuff 
Echo  Something More stuff 
Foxtrot  Something More stuff 

File2: 
Alfa  Something More stuff 
Bravo  Something More stuff 
Echo  Something More stuff 
Foxtrot  Something More stuff 

File3: 
Alfa  Something More stuff 
Bravo  Something More stuff 
Charlie  Something More stuff 
Delta  Something More stuff 
Echo  Something More stuff 

Output: 
FileName1 FileName2 FileName3 
========= ========= ========= 
Alfa   Alfa   Alfa 
      Bravo  Bravo 
Charlie     Charlie 
Delta      Delta 
Echo   Echo   Echo 
Foxtrot  Foxtrot 

回答

1

這裏有一個辦法做到這一點,除了一些小的格式問題等:

awk ' 
    { 
    exists[$1] = 1; 
    files[$1,ARGIND] = 1; 
    } 
    END { 
    for (i=1; i<ARGC; ++i) { 
     printf("%-20s",ARGV[i]) 
    } 
    printf("\n"); 
    for (i=1; i<ARGC; ++i) { 
     printf("%-20s","=================") 
    } 
    printf("\n"); 
    for (name in exists) { 
     for (i=1; i<ARGC; ++i) { 
     if (files[name,i]) { 
      printf("%-20s",name); 
     } 
     else { 
      printf("%-20s",""); 
     } 
     } 
     printf("\n"); 
    } 
    } 
' file1 file2 file3 
+0

該腳本將獲取頭部和右列的長度,但列完全充滿空白。 – user287424 2012-08-14 02:17:20

+0

奇怪的是,我沒有看到與我的測試。這是與GNU Awk 4.0.1。 – 2012-08-14 02:22:25

+0

解決了它。默認情況下,我在Debian上使用mawk。指定的gawk,它的工作原理。排序全部混亂,但很容易重新排序。什麼導致了未分類? – user287424 2012-08-14 02:40:34