comparing two files 
Author Message
 comparing two files

Hi,

How do I match a field from a file to a field from another file and do
the replacement once it matches?

I have here File1.  I want to compare the first field of File1 to the
first field of File2. If it matches, the first field in File1 will be
replaced by the second field of File2 as shown in the output.  If it
doesn't matches, it will stay as is.

File1:

100 12 34 AAA 11
102 32 67 AAA 10
100 24 45 BBB 21
99 10 67 GGG 59
999 54 98 VVV 00
0 0 1 HHH 11

File2:

1 430
2 999
.
.
.
90 500
91 403
92 217
93 56
94 31
96 129
97 53
98 44
99 169
100 700
101 10
102 36
103 77
104 88
105 90
.
.
.
999 101
1000 2229

Output:

700 12 34 AAA 11
36 32 67 AAA 10
700 24 45 BBB 21
169 10 67 GGG 59
101 54 98 VVV 00
0 0 1 HHH 11

What I have in mind is to paste both files to another file then do the
comparisons on that new file.  But is is possible to do this without
using paste?
--
                 ~ A drop of ink makes millions think. ~
                 ????????????????????
                             Albert M. Ollosa
                 ???????????????????



Fri, 28 Sep 2001 03:00:00 GMT  
 comparing two files

Quote:

> have here File1.  I want to compare the first field of File1 to the
>first field of File2. If it matches, the first field in File1 will be
>replaced by the second field of File2 as shown in the output.  If it
>doesn't matches, it will stay as is.

Does your system have the join command? If so, that tool was made for this sort
of task. While you could write this in awk, why bother if you don't have to?


Fri, 28 Sep 2001 03:00:00 GMT  
 comparing two files


% How do I match a field from a file to a field from another file and do
% the replacement once it matches?

If the second file isn't too big, you can load it into an array, then
test whether $1 is an index in the array:
 BEGIN { while ((getline < "File2") > 0) vals[$1] = $2 }
 $1 in vals { $1 = vals[$1] }
 1

--

Patrick TJ McPhee
East York  Canada



Sat, 29 Sep 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to compare two files line-by-line

2. how to compare two columns in two files?

3. reading two files and comparing them

4. : Comparing facts from two files

5. read two sets of data from two spread sheet files to two arrays problem

6. Writing two equal files without creating two files in dct

7. Comparing file objects: what is compared?

8. Comparing two databases.

9. Comparing fields in a two records

10. comparing two arrays with same length

11. comparing two dbfs record by record

12. Comparing two commercial versions of FORTH

 

 
Powered by phpBB® Forum Software