2017-09-01 28 views
-1

我希望有人能帮助我。 希望我能用一些选项(或其他简单的命令)通过“粘贴”或“awk”命令来完成这项任务。结合文本文件列明智与不同数量的行

我有很多不同数量的文件。 我想列这些文件明智的,但没有成功,因为我喜欢。问题在下面。

F1.txt 

1 549 15981 
2 835 19591 
3 322 3896 
4 298 3778 

F2.txt 

1 549 15981 
2 835 19591 
3 322 3896 
4 298 3778 
5 16 202 

我想要的不过outFile是

1 549 15981 1 549 15981 
2 835 19591 2 835 19591 
3 322 3896 3 322 3896 
4 298 3778 4 298 3778 
       5 16 202 

但我和我的命令得到 “粘贴F1.txt F2.txt |列-s $ '\ T' -tn” 是

1 549 15981 1 549 15981 
2 835 19591 2 835 19591 
3 322 3896 3 322 3896 
4 298 3778 4 298 3778 
    5 16  202 

如您所见,由于F1没有第五行,所以F2的第五行向右移动。这不是我想要的。希望有人可以帮助解决这个问题。

+1

无法重现,工作正常(需要对齐) – RomanPerekhrest

+1

您的输入文件需要制表符分隔,不以空白分隔。 –

+0

埃德,非常感谢。你解决了我的问题。 – user2182606

回答

0

输入

$ cat f1 
1 549 15981 
2 835 19591 
3 322 3896 
4 298 3778 

$ cat f2 
1 549 15981 
2 835 19591 
3 322 3896 
4 298 3778 
5 16 202 

输出

使用awk,但它会使用array因此可遇到内存问题,如果您的文件过大

$ awk -v OFS="\t" 'FNR==NR{a[FNR]=$0;m=m>length?m:length;next}{print (FNR in a)?a[FNR]:sprintf("%*s",length,""),$0}' f1 f2 
1 549 15981 1 549 15981 
2 835 19591 2 835 19591 
3 322 3896 3 322 3896 
4 298 3778 4 298 3778 
       5 16 202 

说明

awk -v OFS="\t" ' 
       FNR==NR{ 
          a[FNR]=$0;     # save each record of file f1 in array a 
          m=m>length?m:length;  # find max length of line/row from file f1 
          next      # go to next line 
       } 
                 # read file f2 
       { 
        # if value exists in array a for row index then 
        # print array element, else sprintf with the length of max length line 
        # and current line/row/record of file f2 

        print (FNR in a)?a[FNR]:sprintf("%*s",length,""),$0 

       }' f1 f2 

这里使用pr

$ pr -mt f1 f2 
1 549 15981    1 549 15981 
2 835 19591    2 835 19591 
3 322 3896    3 322 3896 
4 298 3778    4 298 3778 
          5 16 202 

尝试SED或其他一些工具来抑制过剩的标签,类似下面

的一种方式3210
0

paste F * | awk'($ 0〜/^\ t /){$ 0 =“\ t”$ 0} {print}'

+0

你可以简化'paste f1 f2 | awk'/^\ t/{$ 0 =“\ t”$ 0} 1'' –

相关问题