Join multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join multiple files
# 1  
Old 01-06-2014
Join multiple files

Hi there,

I am trying to join 24 files (i showed example of 3 files below). They all have 2 columns. The first columns is common to all. The files are tab delimited eg

file 1
Code:
Code:
Code:
rs0001      100e-34
rs0003      2.8e-01
rs008        1.9e-90

file 2
Code:
Code:
rs0001      1.98e-22
rs0004      3.77e-10
rs0003      2.8e-01
rs008        1.9e-90

file 3
Code:
rs0001      81.98e-22
rs0003      42.8e-01

Desire Output
Code:
                 file1              file2             file3
rs0001      100e-34         1.98e-22       81.98e-22
rs0004       NA                3.77e-10       NA
rs0003      2.8e-01          2.8e-01        42.8e-01
rs008        1.9e-90          1.9e-90         NA

I have tried the script but something is definetly wrong
Code:
awk '{ a[$1]=$2; s[$1]=$1; next } {
  s[$1] = s[$1] " | " $2 - a[$1]; a[$1]=$2
}
END{for(i in s) {print s[i]}}' file*.txt


Thanks for your help
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by fat; 01-06-2014 at 11:59 AM..
# 2  
Old 01-06-2014
Try

Code:
$ awk '{A[$1] = A[$1] ? A[$1] OFS $2 : $2}END{for(i in A)print i, A[i]}' file*  | sort

--edit--

Oops looks like desired o/p modified Smilie
Code:
$ awk 'BEGIN{printf "\t"}{A[$1] = A[$1] ?  A[$1] OFS $2 : $2}FNR==1{printf FILENAME OFS}END{printf RS; for(i in A)print i, A[i]}' OFS='\t' file* | sort

          file1      file2       file3    
rs0001    100e-34    1.98e-22    81.98e-22
rs0003    2.8e-01    2.8e-01     42.8e-01
rs0004    3.77e-10
rs008     1.9e-90    1.9e-90


Last edited by Akshay Hegde; 01-06-2014 at 12:18 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 01-06-2014
Nice one Akshay.

I made a modification to allow sorting within the awk statement:

Code:
awk '{A[$1]=A[$1] ? A[$1] OFS $2 : $2}END{for(i in A)print i, A[i] | "sort"}' file*
rs0001 100e-34 1.98e-22 81.98e-22
rs0003 2.8e-01 2.8e-01 42.8e-01
rs0004 3.77e-10
rs008 1.9e-90 1.9e-90

This User Gave Thanks to in2nix4life For This Post:
# 4  
Old 03-18-2014
Hello,

Just some modification to Akshay's code, thanks Akshay for great code.

Code:
awk 'FNR==1{j=j OFS FILENAME} NR==FNR{a[$1];} {a[$1]=a[$1]?a[$1] OFS $2:$2} END{gsub(/^ /,X,j); print j;{for(i in a){print i OFS a[i]}}}' check_file_check_file121213*

Output will be as follows.

Code:
check_file_check_file1212134 check_file_check_file1212135 check_file_check_file1212136
rs0001 100e-34 1.98e-22 81.98e-22
rs0003 2.8e-01 2.8e-01 42.8e-01
rs0004 3.77e-10
rs008 1.9e-90 1.9e-90


Note: Where files check_file_check_file1212134, check_file_check_file1212135 and check_file_check_file1212136 are files file1, file2 and file3 respectively.


Thanks,
R. Singh

Last edited by RavinderSingh13; 03-18-2014 at 03:19 AM..
# 5  
Old 03-18-2014
you may try below python

Code:
import os
import collections
d=collections.defaultdict(list)
cnt=0
for f in sorted(os.listdir('dir')): 
 with open('dir/'+f) as file:
  for line in file:
   t=line.split(" ")
   if t[0] not in d and cnt==1:
    d[t[0]]=['NA']*cnt    
   d[t[0]].append(t[1]) 
 cnt+=1

for i in sorted(d):
 print(i," ".join(map(lambda x: x.replace("\n",""), d[i])),end=" ")
 if len(d[i])<cnt:
  for i in range(cnt-len(d[i])):
   print('NA',end="")
 print("")

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join 2nd column of multiple files

Dear All, I have many files formatted like this: file1.txt: 1/2-SBSRNA4 18 A1BG 3 A1BG-AS1 6 A1CF 0 A2LD1 1 A2M 1160 file2.txt 1/2-SBSRNA4 53 A1BG 1 A1BG-AS1 7 A1CF 0 A2LD1 3 A2M 2780 (5 Replies)
Discussion started by: paolo.kunder
5 Replies

2. Shell Programming and Scripting

Join files on multiple fields

Hello all, I want to join 2 tabbed files on the first 2 fields, and filling the missing values with 0. The 3rd column in each file is constant for the entire file. file1 12658699 ST5 XX2720 0 1 0 1 53039541 ST5 XX2720 1 0 1.5 1 file2 ... (6 Replies)
Discussion started by: sheetalk
6 Replies

3. Shell Programming and Scripting

Join multiple files with filename

Please help, I want to join multiple files based on column 1, and put the missing values as 0. Also the colname in the output should say which file the values came from. FILE1 1 11 2 12 3 13 FILE2 2 22 3 23 4 24 FILE3 1 31 3 33 4 34 FILE1 FILE2 FILE3 1 11 0 31 (1 Reply)
Discussion started by: newbie83
1 Replies

4. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

5. Shell Programming and Scripting

Awk - join multiple files

Is it possible to join all the files with input1 based on 1st column? input1 a b c d e f input2 a b input3 a e input4 c (2 Replies)
Discussion started by: quincyjones
2 Replies

6. Shell Programming and Scripting

Join multiple files by column with awk

Hi all, I searched through the forum but i can't manage to find a solution. I need to join a set of files placed in a directory (~1600) by column, and obtain an output with first and second column common to each file, but following columns are taken from the file in the list (precisely the fourth... (10 Replies)
Discussion started by: macsx82
10 Replies

7. Shell Programming and Scripting

How to join multiple files?

I am trying to join a few hundred files using join. Is there a way to use while read or something else to automate this. My problem is the following. Day 1 City Temp ABC 20 DEF 30 HIJ 15 Day 2 City Temp ABC 22 DEF 29 KLM 5 Day 3 (3 Replies)
Discussion started by: theFinn
3 Replies

8. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

9. UNIX for Dummies Questions & Answers

Join multiple Split files in Unix

Hi, I have a big file of 50GB size. I need copy it to a second ftp from a ftp. I am not able to do the full 50GB transfer as it timesout after some time. SO i am trying to split the file into 5gb each 10 files with the below command. split -b 5368709120 pack.tar.gz backup.gz After I... (2 Replies)
Discussion started by: venu_nbk
2 Replies

10. Shell Programming and Scripting

shell script to join multiple files

I am a new to Linux and try to write a script to join three multiple files. For example, there are three files file1 # comment a Kevin b Vin c Sam file 2 # comment a 10 b 20 c 40 file 3 # comment a blue b yellow (7 Replies)
Discussion started by: bonosungho
7 Replies
Login or Register to Ask a Question