Joining two files based on columns/fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining two files based on columns/fields
# 8  
Old 09-05-2008
NR == FNR is a common AWK idiom: an expression that returns true while reading the first input file (while the total number of input records seen so far is equal to the input record number in the current input file).
# 9  
Old 09-05-2008
Excerption from the awk manual (nawk):

Quote:
The awk utility keeps track of the number of records that have been read so far from the current input file. This value is stored in a built-in variable called FNR. It is reset to zero when a new file is started.
Another built-in variable, NR, is the total number of input records read so far from all files. It starts at zero but is never automatically reset to zero.
Regards
# 10  
Old 09-05-2008
Does the above code work with all the records matched or unmatched?

lets say if i have an example like

file1

1 1 111 222 I 333 101 60 2 1 0 0 0 0 S 1 01 0 60 34340
1 1 111 222 I 333 101 60 3 1 0 0 0 0 S 1 01 0 60 34340
1 1 111 222 U 121 0 65 4 1 0 0 0 0 S 1 01 0 65 34340
1 1 111 222 U 231 0 125 5 1 0 0 0 0 S 1 01 0 125 34340
1 1 111 222 U 234 0 25 6 1 0 0 0 0 S 1 01 0 25 34340
1 1 123 212 I 342 336 150 2 1 0 0 0 0 S 1 01 0 150 34340
1 1 123 212 I 342 336 150 3 1 0 0 0 0 S 1 01 0 150 34340
1 1 123 212 U 421 0 65 4 1 0 0 0 0 S 1 01 0 65 34340
1 1 123 212 U 564 0 125 5 1 0 0 0 0 S 1 01 0 125 34340
1 1 458 878 U 636 0 225 1 1 0 0 Y 0 Y 0 0 0 0 0


file2

1 bb04 1 5153 123 6704 1310 111 SA 423386 0 0000 0000 49.00 14.00 34.00 0.00 00 4382151906251518 0000000000 00000 100091667 392071011 0000000000 0.00 0.00 0.00
1 823b 1 5153 125356926 6704 1615 392085746 SA 458313 0 0000 0000 279.00 34.00 244.00 0.00 00 0000000000 00000 100091684 392091255 0000000000 0.00 0.00 0.00



Here in this case
file1.col3 = file2.col8 and
file1.col4 = file2.col5

so if we find a match, then i would like to get all the fields of file1 along with file2.col7 and file2.col10 into file3

and if we dont find a match, can we output those records to a new file4
# 11  
Old 09-05-2008
Code:
awk -F'\t' 'NR == FNR { _[$8,$5] = $7 FS $10; next }
($3,$4) in _ { print $0 FS _[$3,$4] > "file3"; next }
{ print > "file4" }' file2 file1

# 12  
Old 09-06-2008
radoulov and frankiln....thanks for your answers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. UNIX for Dummies Questions & Answers

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 Bob, Green, 80 Mark, Brown, 70 Tina, Smith, 60 file 2 Bob, Green, 70 Mark, Brown, 60 Tina, Smith, 50 file 3 Bob, Green, 50 Mark, Brown,60 Tina,... (6 Replies)
Discussion started by: A-V
6 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. Shell Programming and Scripting

Joining multiple files based on one column with different and similar values (shell or perl)

Hi, I have nine files looking similar to file1 & file2 below. File1: 1 ABCA1 1 ABCC8 1 ABR:N 1 ACACB 1 ACAP2 1 ACOT1 1 ACSBG 1 ACTR1 1 ACTRT 1 ADAMT 1 AEN:N 1 AKAP1File2: 1 A4GAL 1 ACTBL 1 ACTL7 (4 Replies)
Discussion started by: seqbiologist
4 Replies

7. UNIX for Dummies Questions & Answers

any script for joining files based on simple conditions

Condition1; If NPID and IndID of both input1 and input2 are same take all the vaues relevant to them and print together as output Condition2; IDNo in output: Take the highly repeated same letter of similar NPID-IndID as *1* Second highly repeated same letter... (0 Replies)
Discussion started by: stateperl
0 Replies

8. Shell Programming and Scripting

joining files based on key column

Hi I have to join two files based on 1st column where 4th column of a2.txt=at and take 2nd column of a1.txt and 3rd column of a2.txt and check against source files ,if matches list those source file names. a1.txt a1|20090809|20090810 a2|20090907|20090908 a2.txt a1|d|file1.txt|at... (9 Replies)
Discussion started by: akil
9 Replies

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

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