Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have two files
I need to create a third file (file3) that contains third field in file2 appended to the beginning of the matching record of file1. If there is no match, the inserted value = the value not found.
Here is an example of file1
“$AAA”,”SYSA”,”VALU1”
“$ABA”,”SYSA”,”VALU1”
“0AAA”,”SYSA”,”VALU1”
“2AAA”,”SYSA”,”VALU1”
“3AAA”,”SYSA”,”VALU1”
Here is an example of file2$AAA,1111,P1
$ABA,1111,P200
0AAA,1111,P35
3AAA,1111,P12Here is the desired output
“P1”,“$AAA”,”SYSA”,”VALU1”
“P200”,“$ABA”,”SYSA”,”VALU1”
“P35”,“0AAA”,”SYSA”,”VALU1”
“2AAA”,“2AAA”,”SYSA”,”VALU1”
“P12”,“3AAA”,”SYSA”,”VALU1”Note – in the 4th record, there was no matching value, so the output = the value that could not be found.
I’m trying to use gawk. Any help would be greatly appreciated.
Thanks!

I read file2 into an associative array. Since file2's fields are not surrounded by double quotes, that's what this line is for:
a["\""$3"\""]="\""$1"\""
Let me know if you have any questions.
#!/bin/ksh
awk ' BEGIN { FS=",";
while(getline < "file2" > 0)
a["\""$3"\""]="\""$1"\""
}
{
for (c in a)
if(a[c] == $1)
{
bf=c
break
}
else
bf=$1printf("%s,%s\n", bf, $0)
} ' file11

Thank you for your help.
I'm getting a syntax error ( I think it is the version of AWK). I will play around with it.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |