Merging fields --- Join is not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging fields --- Join is not working
# 1  
Old 05-22-2009
Merging fields --- Join is not working

Hi GUYS sorry for putting simple query. I have tried the methods posted previously in this site but I'm unable to join the similar values in different columns of different files.
I used sort -u file1 and join but no use.??
I'm attaching my inputfiles.Plz chek them
I have two files.
1st file
1234
133
1345
134
23
4555
secondfile
1234 tab kshgjkghj
23 tab drjghfg
134 tab drkhgjgj
1345 tab djghf
4555 tab khdjhgjfg
133tabjghhdgf

output
1234tab1234 tab kshgjkghj
133tabjghhdgf
1345tabdjghf
134tabdrkhgjgj
23tabdrjghfg
4555tab
# 2  
Old 05-22-2009
if both files has same no of lines then sort them then paste them side by side
Code:
sort -k1 file1
sort -k1 file2
paste -d" " file1 file2

# 3  
Old 05-22-2009
no they are not having same no of lines. Second file is huge. You can see the attachments I have posted.
I tried those commands
sort, print, egrep, awk etc. but nopes.
Thanx anyways for your time
# 4  
Old 05-22-2009
then you can try
Code:
while read line ; do
awk '$1'==$line'{print '$line'"\t"$0}' file2
done < file1

# 5  
Old 05-22-2009
Sorry

Hey sorry it's not running at all.
Could you please try if you have any script on my attachment files.Smilie
# 6  
Old 05-22-2009
You haven't made it entirely clear whether your output should only include data from the q1 file, or include data from both files even where it was missing from one. So, here are a few alternatives, pick the one which suits your needs.

The first one uses grep, the \t in the grep command may not work with your version of grep, so replace it with a tab character instead. This one only outputs two columns (from the q2 file) after matching it from q1:

Code:
sed "s/.*/grep '^&\t' q2.txt/" q1.txt | sh

The second one uses awk, so it will be quicker, and outputs three columns. It also only outputs lines found in q1 (in the order they appear in q2):

Code:
awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0 }' q2.txt

The third one is very much like the second, but includes lines from q2 that were not found in q1:

Code:
awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt

# 7  
Old 05-22-2009
Quote:
Originally Posted by repinementer
Hey sorry it's not running at all.
Could you please try if you have any script on my attachment files.Smilie
its running fine
Code:
 
fnsonlu1-/home/> cat file1
1234
133
1345
134
23
4555
fnsonlu1-/home/> cat file2
1234 tab kshgjkghj
23 tab drjghfg
134 tab drkhgjgj
1345 tab djghf
4555 tab khdjhgjfg
133tabjghhdgf
fnsonlu1-/home/l> while read line ; do
awk '$1'==$line'{print '$line'"\t"$0}' file2
done < vv
1234    1234 tab kshgjkghj
1345    1345 tab djghf
134     134 tab drkhgjgj
23      23 tab drjghfg
4555    4555 tab khdjhgjfg

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to combine fields with sort/join

I have a file with two fields in it delimited by a comma. Some of the first fields are duplicates. I am trying to eliminate any duplicate records in the first field, and combine the second fields in the output file. For example, if the input is: Jane,group=A Bob,group=A Bob,group=D... (3 Replies)
Discussion started by: DJR
3 Replies

2. Shell Programming and Scripting

Merging fields in CSV

Hi experts, I have a csv file which has one field (ID) repeated multiple times with corresponding other field values. I need to convert this file in a format where for a ID all other values has to be present in single field. For Eg : Here in below file ID 1 is repeated 3 times with different... (7 Replies)
Discussion started by: bharathbangalor
7 Replies

3. Shell Programming and Scripting

Merging files with common IDs without JOIN

Hi, I am trying to merge information across 2 files. The first file is a "master" file, with all IDS. File 2 contains a subset of IDs of those in File 1. I would like to match up individuals in File 1 and File 2, and add information in File 2 to that of File 1 if they appear. However, if an... (3 Replies)
Discussion started by: hubleo
3 Replies

4. Shell Programming and Scripting

Join fields comparing 4 fields using awk

Hi All, I am looking for an awk script to do the following Join the fields together only if the first 4 fields are same. Can it be done with join function in awk?? a,b,c,d,8,,, a,b,c,d,,7,, a,b,c,d,,,9, a,b,p,e,8,,, a.b,p,e,,9,, a,b,p,z,,,,9 a,b,p,z,,8,, desired output: ... (1 Reply)
Discussion started by: aksijain
1 Replies

5. Shell Programming and Scripting

join fields

My input is as below: 1|2|3|a02 test|303 2|2|4|1002 a05 ind|303 4|3|5|ind|30 Output 1|2|3|a02test|303 2|2|4|a05ind|303 4|3|5|ind|30 I used command: I am getting above output. Is there any simple way using awk to acheive this. Thanks. Please use code tags! (6 Replies)
Discussion started by: Jairaj
6 Replies

6. UNIX for Dummies Questions & Answers

Need help with Join on multiple fields

Hi, I need help with the join command I have 2 files that I want to join on multiple fields. I want to return all records from file 1 I also want empty fields in my joined file if there isn't a match in file 2 I have already sorted them so I know they are in the same order. file1 ... (0 Replies)
Discussion started by: shunter0810
0 Replies

7. Shell Programming and Scripting

Merging two files by comparing three fields

Hi Experts, I need your timely help. I have a problem with merging two files. Here my situation : Here I have to compare first three fields from FILE1 with FILE2. If they are equal, I have to append the remaining values from FILE2 with FILE1 to create the output. FILE1: Class ... (3 Replies)
Discussion started by: Hunter85
3 Replies

8. Shell Programming and Scripting

Merging fields --- Help me plz

INPUT have a file with 2 columns. evry set in a column ends with a symbol //. the first one with something like chr, chr no, chromosome name, cell no. cell no. etc and the second column has values belong to the first columnlike chr Xy, 22, 345,22222 etc. Some clumns have repeated but not... (4 Replies)
Discussion started by: bogu0001
4 Replies

9. Shell Programming and Scripting

merging fields from 2 different files.

File 1 3337304 2 4 DH.ER@TORONTO.CA 20080504 04622 3337305 2 4 A@C.COM 20080504 04622 3337306 2 4 JO@NET.NET 20080504 04622 3337307 2 4 L@GMAIL.COM 20080504 05344 2479201 2 2 ORY@YAHOO.COM 20080504 05344 File 2 ... (5 Replies)
Discussion started by: rudoraj
5 Replies

10. Shell Programming and Scripting

join on multiple fields

Is it possible to do a join on multiple fields of two files? I am trying to do something like join -t, -1 2,3 -2 2,3 -o 2.1,2.2,2.3,1.3 filea fileb I want the join to be on columns 2 and 3 of filea and columns 2 and 3 of fileb. What is hapenning is that the second file that I want to do the join... (1 Reply)
Discussion started by: reggiej
1 Replies
Login or Register to Ask a Question