How to join one file with multiple files in a directory in UNIX?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to join one file with multiple files in a directory in UNIX?
# 15  
Old 04-24-2015
Dear Rudic

I did not bring the whole data set of file1 and file2 here. That's what you think these two file do not match together. The question is file2 have unique number of id in the second column but in file1 there could be repeated time of ids in column first. I want to know why after merging these two file, I do not get the same line in file1 which it had before.
# 16  
Old 04-24-2015
Applying my proposal in post#6 to the above files yields
Code:
awk 'FNR==NR {T[$2]=$0;next} $1 in T {print T[$1], $0}' file2 file1
1       gi|358484498|ref|NW_003764396.1| gi|358484498|ref|NW_003764396.1| 2503 A G 0 1 1
1       gi|358484498|ref|NW_003764396.1| gi|358484498|ref|NW_003764396.1| 381 A G 1 1 2
1       gi|358484513|ref|NW_003764381.1| gi|358484513|ref|NW_003764381.1| 1351 T C 0 1 1

What exactly is wrong with this? It is the entire lines from file1 and file2 combined. It looks exactly like what you requested in post#1. What does "I do not get the same line in file1 which it had before" mean?
# 17  
Old 04-24-2015
Dear Rudic

When I applied your command, I found 1613 common id between file1 and file2. However, file1 has originally 1805 id and file2 has originally 11580 id. because file1 is the master id file, after merging with file2 we expect 11580 lines which I got 1613 line by your command.
# 18  
Old 04-24-2015
It prints only those lines in which $1 resp. $2 do match. If that's not the desired operation, please explain what this (from post#1) means:
Quote:
Now my desire output is join the Second column of file1 with first column of file2.
# 19  
Old 04-24-2015
Dear Rudic

As my first post, it is clear I want to join the common id between these two files (file1 and file2). But the point is file2 has id with replication and file1 just have unique number for each id. Usually after join two files, the unique ids should match with their ids which repeated in file1. In conclusion, I am trying to merge file1 to file2 without changing the lines of file2.
# 20  
Old 04-24-2015
As in your 1st post; it is NOT clear.

And, I repeat:
Quote:
Please show us a sample set of representative input files and the output (or outputs) that should be produced from those input files.
If you are unwilling to select a representative set of data from your input files that shows lines that should match, lines that should not match, and lines that have multiple matches; and show us the exact output you want from that sample input, then we are all wasting our time here.

Show us sample input. Show us the exact output that should be produced from that sample input If you are unwilling to do that, I will close this thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Comparing two files in UNIX and create a new file similar to equi join

I have 2 files namely branch.txt file & RXD.txt file as below Ex:Branch.txt ========================= B1,Branchname1,city,country B2,Branchname2,city,country B3,Branchname3,city,country B4,Branchname4,city,country B5,Branchname5,city,country RXD file : will... (11 Replies)
Discussion started by: satece
11 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. Shell Programming and Scripting

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 rs0001 100e-34 rs0003 2.8e-01 rs008 1.9e-90 file 2 rs0001 1.98e-22 rs0004 3.77e-10... (4 Replies)
Discussion started by: fat
4 Replies

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question