Joining different columns from multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Joining different columns from multiple files
# 1  
Old 01-24-2014
Question Joining different columns from multiple files

Hello again,
I am trying to join 3rd column of 3 files into the end on one file and save it separately... my data looks like this
file 1
Code:
Bob, Green, 80
Mark, Brown, 70
Tina, Smith, 60

file 2
Code:
Bob, Green, 70
Mark, Brown, 60
Tina, Smith, 50

file 3
Code:
Bob, Green, 50
Mark, Brown,60
Tina, Smith, 70

file 4
Code:
Bob, Green, 60
Mark, Brown, 70
Tina, Smith, 80

desired output
Code:
Bob, Green, 80, 70, 50, 60
Mark, Brown, 70, 60, 60, 70
Tina, Smith, 60, 50, 70, 80

I have tried join for two file (below) but it seems not to work for more than two files
Code:
join -1 1 -2 1 -o 1.1 1.2 1.3 2.3 file1.csv file2.csv > combine.csv

I know I can use 2, 3 joins to make them work but I was looking for a nicer way to combine them
Can you please help me in this regards
Thanks

Last edited by Scrutinizer; 01-24-2014 at 10:57 AM.. Reason: changed quote tags to code tags
# 2  
Old 01-24-2014
join command will work only with 2 files.

Use awk instead:
Code:
awk -F, '
        {
                A[$1 FS $2] = A[$1 FS $2] ? A[$1 FS $2] FS $NF : $NF
        }
        END {
                for ( k in A )
                        print k FS A[k]
        }
' file[1-4]

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-24-2014
thank you very much for the help
the code above was printing two sets of names and family names so I changed the code to have only 1 set by
Code:
 print A[k]

however, when I put it in the loop in both cases it returns a file with 3 columns only
Code:
FILES="output/*"
for W in $FILES
do
	doc=$(basename $W)
	awk -F, '{A[$1 FS $2] = A[$1 FS $2] ? A[$1 FS $2] FS $NF : $N} END {for ( k in A ) print A[k]}'  $W > output-4.csv 
done

do u have any idea y?

Last edited by A-V; 01-24-2014 at 11:00 AM..
# 4  
Old 01-24-2014
Thanks Yoda for great code. Could you please explain the same.
Will be grateful to you.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-24-2014
Quote:
Originally Posted by A-V
however, when I put it in the loop in both cases it returns a file with 3 columns only. do u have any idea y?
If you put that code in a shell loop, it will read only one file at a time, so the awk array gets reset for each file.
This User Gave Thanks to Yoda For This Post:
# 6  
Old 01-24-2014
Dear Yoda,
Thanks a lot for the help... I dont know how to do that but I will have a look on the internet and hopefully will be able to find the solution
# 7  
Old 01-25-2014
Try this one instead :
Code:
awk -F, '{A[$1 FS $2] = A[$1 FS $2] ? A[$1 FS $2] FS $NF : $N} END {for ( k in A ) print A[k]}' output/* > output-4.csv

This User Gave Thanks to tukuyomi For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CSV joining and checking multiple files

Hello, For our work we use several scripts to gather/combine data for use in our webshop. Untill now we did not had any problems but since a couple days we noticed some mismatches between imports. It happened that several barcodes where matched even though it was a complete other product. Of... (19 Replies)
Discussion started by: SDohmen
19 Replies

2. Shell Programming and Scripting

Joining Two Files Matching Two Columns

Hi All, I am looking to join two files where column 1 of file A matches with column 1 of file B and column 5 of files A matches with column 2 of file B. After joining the files based on above condition, out should contain entire line of file A and column 3, 4 and 5 of file B. Here is sample... (8 Replies)
Discussion started by: angshuman
8 Replies

3. Shell Programming and Scripting

Joining files using awk not extracting all columns from File 2

Hello All I'm joining two files using Awk by Left outer join on the file 1 File 1 1 AA 2 BB 3 CC 4 DD File 2 1 IND 100 200 300 2 AUS 400 500 600 5 USA 700 800 900 (18 Replies)
Discussion started by: venkat_reddy
18 Replies

4. Shell Programming and Scripting

Other alternative for joining together columns from multiple files

Hi again, I have monthly one-column files of roughly around 10 years. Is there a more efficient way to concatenate these files column-wise other than using paste command? For instance: file1.txt 12 13 15 12 file2.txt 14 15 18 19 file3.txt 20 21 (8 Replies)
Discussion started by: ida1215
8 Replies

5. Shell Programming and Scripting

NR==FNR trick for joining columns from two files

foo.txt 1 rs2887286 0 1145994 C T 1 rs1240743 0 1323299 C A 1 rs1695824 0 1355433 G T 1 rs3766180 0 1468016 G A 1 rs7519837 0 1500664 A G 1 rs2272908 0 ... (12 Replies)
Discussion started by: genehunter
12 Replies

6. UNIX for Dummies Questions & Answers

Joining string on multiple files

Hi guys, I am a forum (and a bit of a unix) newbie, and I currently have a tricky problem lying ahead of me. I have multiple files, and I am looking to join the files on the first column. Example: File 1 andy b 100 amy c 200 amy d 300 File 2 andy c 200 amy c 100 clyde o 50 ... (3 Replies)
Discussion started by: jdr0317
3 Replies

7. Shell Programming and Scripting

joining multiple files into one while putting the filename in the file

Hello, I know how to join multiple files using the cat function. I want to do something a little more advanced. Basically I want to put the filename in the first column... One thing to note is that the file is tab delimited. e.g. file1.txt joe 1 4 5 6 7 3 manny 2 3 4 5 6 7 ... (4 Replies)
Discussion started by: phil_heath
4 Replies

8. UNIX for Dummies Questions & Answers

Joining files based on multiple keys

I need a script (perl or awk..anything is fine) to join 3 files based on three key columns. The no of non-key columns can vary in each file. The columns are delimited by semicolon. For example, File1 Dim1;Dim2;Dim3;Fact1;Fact2;Fact3;Fact4;Fact5 ---- data delimited by semicolon --- ... (1 Reply)
Discussion started by: Sebben
1 Replies

9. Shell Programming and Scripting

Joining two files based on columns/fields

I've got two files, File1 and File2 File 1 has got combination of col1, col2 and col3 which comes on file2 as well, file2 does not get col4. Now based on col1, col2 and col3, I would like to get col4 from file1 and all the columns from file2 in a new file Any ideas? File1 ------ Col1 col2... (11 Replies)
Discussion started by: rudoraj
11 Replies

10. Shell Programming and Scripting

Joining columns from two files, if the key matches

I am trying to join/paste columns from two files for the rows with matching first field. Any help will be appreciated. Files can not be sorted and may not have all rows in both files. Thanks. File1 aaa 111 bbb 222 ccc 333 File2 aaa sss mmmm ccc kkkk llll ddd xxx yyy Want to... (1 Reply)
Discussion started by: sk_sd
1 Replies
Login or Register to Ask a Question