Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to use "awk" to print columns from different files in separate columns?
# 1  
Old 08-01-2019
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.



Code:
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!
# 2  
Old 08-01-2019
Code:
awk '{printf $6 " "; } END {print ""}' *.label > fname.txt


Last edited by rdrtx1; 08-01-2019 at 06:22 PM.. Reason: print only field 6 of each line in files?, also removed exit statement in order to process all lines in all files.
# 3  
Old 08-01-2019
Thanks for the reply! I tried it but it produced an empty output.
# 4  
Old 08-01-2019
Please post the output of ls -l in the directory you are executing your script.
# 5  
Old 08-01-2019
Try

Code:
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.
# 6  
Old 08-01-2019
Code:
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

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input and output as well as when displaying code segments.

Last edited by Don Cragun; 08-01-2019 at 05:16 PM..
# 7  
Old 08-01-2019
Hi Frastra,
Note that the code in your script:
Code:
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:
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
Code:
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #677
Difficulty: Medium
The IEEE does not test equipment for compliance with their standards.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Why awk print is strange when I set FS = " " instead of FS = "\t"?

Look at the following data file(cou.data) which has four fields separated by tab. Four fields are country name, land area, population, continent where it belongs. As for country name or continent name which has two words, two words are separated by space. (Data are not accurately... (1 Reply)
Discussion started by: chihuyu
1 Replies

4. Shell Programming and Scripting

Awk find in columns with "if then" statement and print results

I have a file1.txt file1.txt F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true" F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true" U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true"... (4 Replies)
Discussion started by: charles33
4 Replies

5. Shell Programming and Scripting

sort, columns, no result! can I print files of "planes"?

hi, please can I ask you for some help? I have data from 3D situation, x y z value I'd like to use gnuplot to generate maps of the value in the planes z=0 to z=1 for example, my file looks like -0,012 0,0060 0,0 0,13972813076023477 -0,012 0,0064319163 4,2894483E-4 ... (1 Reply)
Discussion started by: kocour
1 Replies

6. UNIX for Dummies Questions & Answers

replace "," with "." only in specific columns of a file?

Hi all, I have this text file containing 9 columns separated by space. The 8th columns contains the numbers. C1 C2 C3 C4 C5 C6 C7 C8 C9 er rt yt gh iu nk il 0.07 xs yt lr ty bg iu zk nh 0,0005 lt ...etc. I want to replace the comma with full stop only in 8th coloumn. the output... (8 Replies)
Discussion started by: Unilearn
8 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

defining a variable using multiple "entries/columns" within a line using read

Hello All, I am trying to figure out how to use the read command. I have a txt file that has rows looking something like this: 1 2 3 4 5 6 7 8 9 where each number represents an entry of various characters deliminated by tabs. My goal is to set entries 2-7 as a variable/string that I... (3 Replies)
Discussion started by: Torinator
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

Featured Tech Videos