How to use "awk" to print columns from different files in separate columns?

Hi,

I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.)

I tried this code but works only partially because it copied and pasted 50 rows of each column instead of the total number whis is 114.

awk 'FNR==1{f++}{a[f,FNR]=$6}END{for(x=1;x<=FNR;x++){for(y=1;y<ARGC;y++)printf("%s ",a[y,x]);print ""}}' *.label > fname.txt

Thanks!

awk '{printf $6 " "; } END {print ""}' *.label > fname.txt

Thanks for the reply! I tried it but it produced an empty output.

Please post the output of ls -l in the directory you are executing your script.

Try

paste <(cut -d" " -f6 file1) <(cut -d" " -f6 file2) <(cut -d" " -f6 file3)

I can't see any reason why your code snippet should stop working a line 50 - I tried and it worked up to 1400 lines.

francesca@shalimpc:~/Desktop/ROI-scimmie/ROI-scimmie/scimmia-1-Celia/label/FF/V6$ paste <(cut -d" " -f6 rh-FF-V6-10-FF.label) <(cut -d" " -f6 rh-FF-V6-12-FF.label) <(cut -d" " -f6 rh-FF-V6-14-FF.label)
        
48    48    48
-0.430436    1.800498    1.291823
-0.420545    2.075040    1.187987
-0.372471    1.584477    1.310843
-0.336654    1.733547    1.256660
-0.209870    1.177998    1.238225
-0.155597    1.142074    1.292147
-0.022510    0.566363    1.035261
-0.421321    1.652684    1.335302
-0.428050    1.725078    1.332900
-0.321378    1.382081    1.347380
-0.373038    1.492446    1.420269
-0.133966    0.980780    1.194646
-0.030317    0.677223    1.009947
0.126822    0.069327    0.515428
0.298326    -0.424181    -0.562148
0.251213    -0.469369    -0.344842
0.290995    -0.485099    -0.820039
-0.303998    1.297450    1.422969
-0.275101    1.269499    1.451483
-0.156093    1.061603    1.303209
-0.126584    1.079174    1.382680
0.083088    0.621172    0.888184
0.047205    0.877263    1.151542
0.328560    0.151505    -0.040908
0.277034    0.166088    0.171452
0.320698    -0.023553    -0.304491
0.296364    -0.185507    -0.194502
0.348793    0.288864    -0.547963
0.273967    0.585501    0.629874
0.211306    0.742472    0.897028
0.353276    0.434319    0.038047
0.367151    0.671365    0.380329
0.365127    0.850012    0.694780
0.368822    0.720077    -0.175395
0.365795    0.825067    -0.384303
0.372548    0.654244    -0.585398
0.418335    0.981613    0.186605
0.458616    1.034648    0.462049
0.510201    1.087841    0.616021
0.404362    1.186138    -0.008441
0.376120    1.148764    -0.212652
0.373522    1.050479    -0.393568
0.482250    1.210870    0.334392
0.548836    1.255677    0.440449
0.491018    1.363566    0.172108
0.407628    1.370742    -0.077822
0.389008    1.287857    -0.282209
0.440773    1.432027    -0.093359
francesca@shalimpc:~/Desktop/ROI-scimmie/ROI-scimmie/scimmia-1-Celia/label/FF/V6$ francesca@shalimpc:~/Desktop/ROI-scimmie/ROI-scimmie/scimmia-1-Celia/label/FF/V6$ paste <(cut -d" " -f6 rh-FF-V6-10-FF.label) <(cut -d" " -f6 rh-FF-V6-12-FF.label) <(cut -d" " -f6 rh-FF-V6-14-FF.label)




francesca@shalimpc:~/Desktop/ROI-scimmie/ROI-scimmie/scimmia-1-Celia/label/FF/V6$ awk 'FNR==1 {printf $6 " "; exit; } END {print ""}' *.label > fname.txt
francesca@shalimpc:~/Desktop/ROI-scimmie/ROI-scimmie/scimmia-1-Celia/label/FF/V6$ ls -l
total 200
-rw-rw-r-- 1 francesca francesca  656 Aug  1 14:47 fname_scimmia1_Celia_FF_V6.txt
-rw-rw-r-- 1 francesca francesca    2 Aug  1 18:47 fname.txt
-rw-r--r-- 1 francesca francesca 5339 Jul  7 14:40 lh-FF-V6-10-FF.label
-rw-r--r-- 1 francesca francesca 5300 Jul  7 14:43 lh-FF-V6-12-FF.label
-rw-r--r-- 1 francesca francesca 5215 Jul  7 14:53 lh-FF-V6-14-FF.label
-rw-r--r-- 1 francesca francesca 5363 Jul  7 16:40 lh-FF-V6-16-FF.label
-rw-r--r-- 1 francesca francesca 5311 Jul  7 16:43 lh-FF-V6-18-FF.label
-rw-r--r-- 1 francesca francesca 5277 Jul  7 16:52 lh-FF-V6-20-FF.label
-rw-r--r-- 1 francesca francesca 5363 Jul  7 17:48 lh-FF-V6-22-FF.label
-rw-r--r-- 1 francesca francesca 5316 Jul  7 18:08 lh-FF-V6-24-FF.label
-rw-r--r-- 1 francesca francesca 5340 Jul  7 16:46 lh-FF-V6-2-FF.label
-rw-r--r-- 1 francesca francesca 5385 Jul  7 18:11 lh-FF-V6-4-FF.label
-rw-r--r-- 1 francesca francesca 5354 Jul  7 18:14 lh-FF-V6-6-FF.label
-rw-r--r-- 1 francesca francesca 5291 Jul  7 18:18 lh-FF-V6-8-FF.label
-rw-r--r-- 1 francesca francesca 5336 Jul  6 19:57 lh-FF-V6-FF-1.label
-rw-r--r-- 1 francesca francesca 5291 Jul  6 20:00 lh-FF-V6-FF-2.label
-rw-r--r-- 1 francesca francesca 5253 Jul  6 20:05 lh-FF-V6-FF-3.label
-rw-r--r-- 1 francesca francesca 5267 Jul  6 20:10 lh-FF-V6-FF-4.label
-rw-r--r-- 1 francesca francesca 2213 Jul  3 17:30 rh-FF-V6-10-FF.label
-rw-r--r-- 1 francesca francesca 2197 Jul  3 17:34 rh-FF-V6-12-FF.label
-rw-r--r-- 1 francesca francesca 2165 Jul  3 17:40 rh-FF-V6-14-FF.label
-rw-r--r-- 1 francesca francesca 2196 Jul  3 17:43 rh-FF-V6-16-FF.label
-rw-r--r-- 1 francesca francesca 2219 Jul  3 17:47 rh-FF-V6-18-FF.label
-rw-r--r-- 1 francesca francesca 2238 Jul  3 17:56 rh-FF-V6-20-FF.label
-rw-r--r-- 1 francesca francesca 2244 Jul  3 18:00 rh-FF-V6-22-FF.label
-rw-r--r-- 1 francesca francesca 2242 Jul  3 18:03 rh-FF-V6-24-FF.label
-rw-r--r-- 1 francesca francesca 2244 Jul  3 17:51 rh-FF-V6-2-FF.label
-rw-r--r-- 1 francesca francesca 2244 Jul  3 18:08 rh-FF-V6-4-FF.label
-rw-r--r-- 1 francesca francesca 2243 Jul  3 18:13 rh-FF-V6-6-FF.label
-rw-r--r-- 1 francesca francesca 2234 Jul  3 18:17 rh-FF-V6-8-FF.label
-rw-r--r-- 1 francesca francesca 2242 Jul  7 13:26 rh-FF-V6-FF-1.label
-rw-r--r-- 1 francesca francesca 2196 Jul  7 13:29 rh-FF-V6-FF-2.label
-rw-r--r-- 1 francesca francesca 2196 Jul  7 13:33 rh-FF-V6-FF-3.label
-rw-r--r-- 1 francesca francesca 2196 Jul  7 13:37 rh-FF-V6-FF-4.label

Hi Frastra,
Note that the code in your script:

END{for(x=1;x<=FNR;x++) ... ... ...

takes the value of FNR from the last record read from the last input file you are reading.

Unless each of your *.label input files contain exactly the same number of lines, this is probably not what you want.

H rdrtx1,
The exit in your code:

awk '{printf $6 " "; exit; } END {print ""}' *.label > fname.txt

will cause your script to use the contents of the sixth field on the first line of the first input file followed by a space as a

printf

format string (which might or might not be a valid format string), and a newline and then exit your script. Since the ls output shows that the length of your output is two characters, I would assume that the sixth field of the first line of the first input file was empty.