Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to compare fields from two files and replace 1 field? 1

Status
Not open for further replies.

swa75

Technical User
Aug 20, 2002
14
FR
Hello everybody!

i'm new on this forum and i have a problem that has already been solved but that's not clear for me. So I have two files :

FILE1 :
a:4:fdsf:fdze
b:5:greg:fdee
c:1:jyth:gtre
...

FILE2 :
gfegj jyut 5 eazert
gfdg ytyhj 6 area
greatr juyj 1 ytjuy
geqg hyth 4 htrhtr

and I want to find the 2nd field of file1 in file2 (3rd field)and replace the value of the 2nd field of file1 by the value of the 1st field of file2.

and so I want to obtain :
FILE1 :
a:geqg:fdsf:fdze
b:gfegj:greg:fdee
c:greatr:jyth:gtre

I hope it's clear and thanks if you can give me some help.

 
Load file two's first and third fields into an
array:
BEGIN {
while ((getline < filename) > 0) {
array[$3] = $1
}
}

Then do a search and replace.
{
for (i in array) {
if ($2 == i) {
$2 = array
}
}
print
}
Good Luck
 
just another question. in your script where do you specify the path of file1? I've understood for file2 but not for file1.
 
At the very end:

BEGIN {
while ((getline < file2) > 0) {
array[$3] = $1
}
}

Then do a search and replace.
{
for (i in array) {
if ($2 == i) {
$2 = array
}
}
print
}' file1

Note that this is untested, try at your own peril ;)
 
I'll use it carefully, but if it crashes everything I will find you [wink]
 
It works very well!
you're lucky [peace][thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top