Match values from 2 files and append certain fields


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Match values from 2 files and append certain fields
# 1  
Old 03-16-2011
Match values from 2 files and append certain fields

Hi,

I need help on appending certain field in my file1.txt based on matched patterns in file2.txt using awk or sed.

The blue color need to match with one of the data in field $2 in file2.txt. If match, BEGIN and FINISHED value in red will have a new value from field $3 and $4 accordingly.


file1.txt

Code:
ID   N_4736
NAME   N_4736
BEGIN  544
FINISHED  550
COM    Safe  

ID    N_32
NAME   N_32
BEGIN    872
FINISHED   1027
COM   Crash

Input file2.txt
Code:
test1     4754    120      160
test1     4736    500      600
test1     5487    1478     200      
test1     32      800      1000

The output should look like this:-

Code:
D   N_4736
NAME   N_4736
BEGIN  500
FINISHED   600
COM    Safe  

ID    N_32
NAME   N_32
BEGIN    800
FINISHED  1000
COM   Crash

I really don't know how to do this. Smilie pls..appreciate your help or advise on this. Thanks
# 2  
Old 03-17-2011
Well, it is a join and update, really join and create new file. You might want to avoid the cartesion join NxM costs of unsorted data. If you grock the SQL mind, rows/tuples have no implicit order or position vertically, and columns/attributes have no implicit horizontal order/position. You could put all the new data in associative arrays in bash or perl, and then look it up as you go through the old replacing to write the new, which works for small new data. An associative array looks like a vector or array my_array_name[xxx] but xxx does not have to be integer 0 to (n-1), (or one to in COBOL :-) ), it can be any string.
  • I guess you need the format, but
  • it might be easier to turn it into lines, join them, and turn it back into blocks. You could even leave it in lines, and just present it in blocks.
  • Can the output be key-sorted, or does it have to stay in the original order?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching two fields in two csv files, create new file and append match

I am trying to parse two csv files and make a match in one column then print the entire file to a new file and append an additional column that gives description from the match to the new file. If a match is not made, I would like to add "NA" to the end of the file Command that Ive been using... (6 Replies)
Discussion started by: dis0wned
6 Replies

2. UNIX for Beginners Questions & Answers

awk match two fields in two files

Hi, I have two TEST files t.xyz and a.xyz which have three columns each. a.xyz have more rows than t.xyz. I will like to output rows at which $1 and $2 of t.xyz match $1 and $2 of a.xyz. Total number of output rows should be equal to that of t.xyz. It works fine, but when I apply it to large... (6 Replies)
Discussion started by: geomarine
6 Replies

3. Shell Programming and Scripting

Match value in column and append file with new values

Hi, I need help to match two files based on two columns. file_1 ID AA An Ca Ele Pro Su Ot Tra g13950 No No Yes No Yes Yes Yes Yes g05760 Yes No No No No Yes Yes Yes g12640 No No No No No No No No k17720 No Yes No No No No No Yes g05640 Yes Yes Yes No No Yes Yes Yes file_2 ... (8 Replies)
Discussion started by: redse171
8 Replies

4. Shell Programming and Scripting

Add fields in different files only if some fields between them match

Hi everybody (first time posting here) I have a file1 that looks like > 1,101,0.1,0.1 1,26,0.1,0.1 1,3,0.1,0.1 1,97,0.5,0.5 1,98,8.1,0.218919 1,99,6.2,0.248 2,101,0.1,0.1 2,24,3.1,0.147619 2,25,23.5,0.559524 2,26,34,0.723404with 762 lines.. I have another 'similar' file2 > ... (10 Replies)
Discussion started by: murpholinox
10 Replies

5. Shell Programming and Scripting

how to match fields from different files in PERL

Howdy! I have multiple files with tab-separated data: File1_filtered.txt gnl|Amel_4.0|Group3.29 1 G R 42 42 60 15 ,.AAA.aa,aa.A.. hh00/f//hD/h/hh gnl|Amel_4.0|Group3.29 2 C Y 36 36 60 5 T.,T, LggJh gnl|Amel_4.0|Group3.29 3 A R 27 27 60 9 Gg,,.gg., B6hcc22_c File2_filtered.txt ... (3 Replies)
Discussion started by: sramirez
3 Replies

6. Shell Programming and Scripting

Match two files and divide fields

I have two files that have the date field in common. I request your help with some script that divide each field value from file1 by the correspond field value of the file2 only when the field date is equal in both files. Thanks in advance ! This is a sample of the files file 1 12/16/2010,... (2 Replies)
Discussion started by: csierra
2 Replies

7. Shell Programming and Scripting

Compare two files based on values of fields.

Hi All, I have two files and data looks like this: File1 Contents #Field1,Field2 Dist_Center_file1.txt;21 Dist_Center_file3.txt;20 Dist_Center_file2.txt;20 File2 Contents (*** No Header ***) Dist_Center_file1.txt;23 Dist_Center_file2.txt;20 Dist_Center_file3.txt;20 I have... (4 Replies)
Discussion started by: Hangman2
4 Replies

8. Shell Programming and Scripting

Match data based on two fields, and append to a line

I need to write a program to do something like a 'vlookup' in excel. I want to match data from file2 based on two fields (where both match) in file1, and for matching lines, add the data from two of the fields from file2 to file1. If anyone knows something in perl or awk that can do this, I'd be... (20 Replies)
Discussion started by: jamessmith01
20 Replies

9. Shell Programming and Scripting

plus values from two files differient fields.

Hi Everyone, I have two files: filea: Sun Jun 21 14:37:56 2009 1 2 3 Sun Jun 21 11:47:16 2009 2 3 4 fileb: Sun Jun 21 14:37:56 2009 1 2 3 Sun Jun 21 11:47:17 2009 33 44 55 The output is filec: Sun Jun 21... (4 Replies)
Discussion started by: jimmy_y
4 Replies

10. UNIX for Dummies Questions & Answers

Match & append the files

Hi All, I have a problem in appending the files File 1 0.0000001 0.500000039894 0.0000002 0.500000079788 0.0000003 0.500000119683 0.0000004 0.500000159577 0.0000005 0.500000199471 0.0000006 0.500000239365 0.0000007 0.500000279260 0.0000008 0.500000319154 0.0000009 0.500000359048... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question