merge files with same row values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merge files with same row values
# 1  
Old 08-31-2010
merge files with same row values

Hi everyone,

I'm just wondering how could I using awk language merge two files by comparison of one their row.
I mean, I have one file like this:

file#1:

21/07/2009 11:45:00 100.0000000 27.2727280
21/07/2009 11:50:00 75.9856644 25.2492676
21/07/2009 11:55:00 51.9713287 23.2258072
21/07/2009 12:00:00 27.9569893 21.2023468
21/07/2009 12:05:00 27.9244041 21.5249271

and

file#2:
27/01/2009 11:00:00 1008
27/01/2009 12:00:00 1008
27/01/2009 13:00:00 1008
27/01/2009 14:00:00 1008
27/01/2009 15:00:00 1009
27/01/2009 16:00:00 1009
27/01/2009 17:00:00 1010
27/01/2009 18:00:00 1010

And finally I would like that the final output would be the merge of two file when the fields $1 and $2 were equal between both.

Thanks in advance.

Toni
# 2  
Old 08-31-2010
Check join command
# 3  
Old 08-31-2010
Code:
nawk '{idx=$1 SUBSEP $2} FNR==NR{f2[idx]=$3;next} {print $0 (idx in f2)? OFS f2[idx]:""}' file2 file1

# 4  
Old 08-31-2010
Quote:
Originally Posted by anbu23
Check join command
join can't join on multiple fields.
@tonet try this:
Code:
awk 'NR==FNR{for (i=3;i<=NF;i++){a[$1$2]=a[$1$2]" "$i}next}$1$2 in a{print $0a[$1$2]}' file1 file2

Files can be specified in any order.

Last edited by bartus11; 08-31-2010 at 05:07 PM..
# 5  
Old 08-31-2010
bartus11, thanks a lot for your response. It works perfectly.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge 4 bim files by keeping only the overlapping variants (unique rs values )

Dear community, I am facing a problem and I kindly ask your help: I have 4 different data sets consisted from 3 different types of array. On each file, column 1 is chromosome position, column 2 is SNP id etc... Lets say I have the following (bim) datasets: x2014: 1 rs3094315... (4 Replies)
Discussion started by: fondan
4 Replies

2. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

3. Shell Programming and Scripting

Merge row based on replicates ID

Dear All, I was wondering if you may help me with an issue. I would like to merge row based on column 1. input file: b1 ggg b2 fff NA NA hhh NA NA NA NA NA a1 xxx a2 yyy NA NA zzz NA NA NA NA NA a1 xxx NA NA a3 ttt NA ggg NA NA NA NA output file: b1 ggg b2 fff NA NA hhh NA NA NA NA NA... (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

4. Shell Programming and Scripting

Help with merge row if share same column info

Input file: 32568 SSO7483 32568 SSO7486 117231 SSO1293 117231 SSO1772 178081 SSO3076 178081 SSO3077 222417 porA-2 222417 porB-2 263778 SSO1245 263778 SSO0509 . . Desired output: 32568 SSO7483,SSO7486 117231 ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

5. Shell Programming and Scripting

Help with merge data at same column but different row inquiry

Hi, Anyone did experience to merge data at same column but different row previously by using awk, sed, perl, etc? Input File: SSO12256 SSO0001 thiD-1 rbsK-1 SSO0006 SSO0007 SSO0008 SSO0009 SSO0010 SSO0011 Desired Output File: (5 Replies)
Discussion started by: perl_beginner
5 Replies

6. Shell Programming and Scripting

How to merge two files with unique values matching.?

I have one script as below: #!/bin/ksh Outputfile1="/home/OutputFile1.xls" Outputfile2="/home/OutputFile2.xls" InputFile1="/home/InputFile1.sql" InputFile2="/home/InputFile2.sql" echo "Select hobby, class, subject, sports, rollNumber from Student_Table" >> InputFile1 echo "Select rollNumber... (3 Replies)
Discussion started by: Sharma331
3 Replies

7. Shell Programming and Scripting

How to merge multiple rows into single row if first column matches ?

Hi, Can anyone suggest quick way to get desired output? Sample input file content: A 12 9 A -0.3 2.3 B 1.0 -4 C 34 1000 C -111 900 C 99 0.09 Output required: A 12 9 -0.3 2.3 B 1.0 -4 C 34 1000 -111 900 99 0.09 Thanks (3 Replies)
Discussion started by: cbm_000
3 Replies

8. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

9. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

10. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies
Login or Register to Ask a Question