本文介绍了如何在bash中一个接一个地打印列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,是否有更好的方法将两列或更多列打印为一列

Is there any better methods to print two or more columns into one column, for example

input.file

input.file

AAA    111
BBB    222
CCC    333

输出:

AAA
BBB
CCC
111
222
333

我只能想到:

cut -f1 input.file >output.file;cut -f2 input.file >>output.file

但是如果有很多列,或者当我想将输出传递给其他命令(例如sort)时,这并不是很好.

But it's not good if there are many columns, or when I want to pipe the output to other commands like sort.

还有其他建议吗?非常感谢你!

Any other suggestions? Thank you very much!

推荐答案

使用awk

awk '{if(maxc<NF)maxc=NF;
      for(i=1;i<=NF;i++){(a[i]!=""?a[i]=a[i]RS$i:a[i]=$i)}
      }
     END{
      for(i=1;i<=maxc;i++)print a[i]
     }' input.file

这篇关于如何在bash中一个接一个地打印列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:54
查看更多