Search code examples
bashawksedmultiple-columnscut

How to print columns one after the other in bash?


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

input.file

AAA    111
BBB    222
CCC    333

output:

AAA
BBB
CCC
111
222
333

I can only think of:

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

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!


Solution

  • With 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