I wanted to organize my dataset by copying the a column from a number of text file and append the file name in the first row of the new files. Here is sample of my data
file1
a a b b
1 2 3 4
file2
c d e f
g h i g
. . .
file3
11 12 23 12
2 4 6 7
filen
n1 n2 n3 n4
nn nm no np
my desired output is to produce 4 data files by collecting respective columns from the datafile.
Output1
file1 file2 file3 filen
a c 11 n1
1 g 2 nn
Output2
file1 file2 file3 filen
a d 12 n2
2 h 4 nm
Output3
file1 file2 file3 filen
b e 23 n3
3 i 6 no
Output4
file1 file2 file3 filen
b f 12 n4
4 g 7 np
I was able to copy and organize in to the four desired files using
awk 'FNR==1{f++}{a[f,FNR]=$1}END{for(x=1;x<=FNR;x++){for(y=1;y<ARGC;y++)printf("%s ",a[y,x]);print ""}}' file* > output1
awk 'FNR==1{f++}{a[f,FNR]=$2}END{for(x=1;x<=FNR;x++){for(y=1;y<ARGC;y++)printf("%s ",a[y,x]);print ""}}' file* > output2
awk 'FNR==1{f++}{a[f,FNR]=$3}END{for(x=1;x<=FNR;x++){for(y=1;y<ARGC;y++)printf("%s ",a[y,x]);print ""}}' file* > output3
awk 'FNR==1{f++}{a[f,FNR]=$4}END{for(x=1;x<=FNR;x++){for(y=1;y<ARGC;y++)printf("%s ",a[y,x]);print ""}}' file* > output4
However, I couldn't append the file name in the first row of the output files.
Any help would be appreciated
paste
command?