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 datafile1 a   a   b   b1   2   3   4file2c   d   e   fg   h   i   g...file311  12  23  122   4   6   7filenn1  n2  n3  n4nn  nm  no  npmy desired output is to produce 4 data files by collecting respective columns from the datafile.Output1file1   file2   file3   filena   c   11  n11   g   2   nnOutput2file1   file2   file3   filena   d   12  n22   h   4   nmOutput3         file1   file2   file3   filenb   e   23  n33   i   6   noOutput4         file1   file2   file3   filenb   f   12  n44   g   7   npI 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* > output1awk '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* > output2awk '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* > output3awk '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* > output4However, I couldn't append the file name in the first row of the output files. Any help would be appreciated
Copy column with file name as column header
text processing;awk;data
If you already have the output files and just need to add a header with the file names to each of them, all you need is something like:header=$(printf %s %s %s %s\n $(awk 'FNR==1{print FILENAME}' file*))for file in output*; do     printf '%s\n%s\n' $header $(cat $file) > tmp &&     mv tmp $file; done