pary.ch
pary.ch

Reputation: 11

read columns from several file and print them in individual columns

I have several text files which each one contains several columns contains numbers e.g:

5 10 6

6 20 1

7 30 4

8 40 3

9 23 1

4 13 6

I want to collect the second column of all files in separate columns. I used this code, it works but print all second columns in a single column.

{awk '{print $3}' > outfile}

How can I print each column in an individual one?

Upvotes: 1

Views: 36

Answers (1)

karakfa
karakfa

Reputation: 67567

$ awk '{a[FNR]=(FNR in a)?a[FNR] OFS $2:$2} 
   END {for(i=1;i<=NR;i++) print a[i]}' file1 file2 ... > outfile

assumes all files have the same number of lines, otherwise alignment will be off.

Upvotes: 4

Related Questions