Reputation: 111
I am trying to compare two files (separted by comma and space) using 3 fields (field 1,2, 5 from file1 and 1,2,5 from file2) if the two files match i want the whole record of file2 concatenated with the last filed of file1 using awk. for example file1:
1, 4, abebe, kebede, 25, 101, 42
1, 4, abebe, debebe, 42, 201, 47
1, 4, abebech, kebede, 17, 33, 57
file2:
1, 4, abebe, kebede, 25, 101, 42
1, 4, Tesse, debo, 25, 101, 42
1, 4, derartu, tulu, 25, 101, 42
output:
42, 1, 4, abebe, kebede, 25, 101, 42
47, 1, 4, Tesse, debo, 25, 101, 42
57, 1, 4, derartu, tulu, 25, 101, 42
I am new for linux.... any help is apprciated
Upvotes: 0
Views: 1598
Reputation: 359935
Since fields 1, 2 and 5 of record 1 in file 1 match all the records in file 2 I have listed the files as arguments in the opposite order in order to get the output you want.
awk 'BEGIN {OFS = ", "} NR == FNR {a[$1, $2, $5] = $NF; next} $1 SUBSEP $2 SUBSEP $5 in a {print a[$1, $2, $5], $0}' file2 file1
The NR == FNR
block forms a loop that reads the file which appears first in the argument list into an array. When the record number (NR
) and the file record number (FNR
) are no longer equal, processing continues to the file which is named as the second argument.
There, the array is checked to see if the fields from the two files match. If so, the corresponding saved field and the current record are output.
Upvotes: 1
Reputation: 212208
My first reading of the problem lends itself to this solution:
awk '{getline t < "file2"; split( t, a );
if( a[1]a[2]a[5] == $1$2$5) print $NF",", t}' file1
But it appears that the question is actually: 'Given file1
in which we know that any record in which fields 1, 2, and 4 are the same the final field is also the same, find all lines in file2
with corresponding fields 1, 2, and 4 and output that line with the final field from file1
prepended. In which case the solution given by Dennis works.
Upvotes: 1