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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK command for 2 file merge

Status
Not open for further replies.

amstel123

Programmer
Feb 10, 2011
3
NL
Hi,

Could anyone help with command wher i need to merge two file with variable number of records based on matching records

File1
*******
F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12
..
..
..
N records



File2
*****
F1|F2|F3|F4|F5|F13|F14|F15
..
..
..
M Records


I need the output as
File3
********
F1|F2|F3|F4|F5|F6|F7|F8|F9|F10|F11|F12|F13|F14|F15

Conditions
1 N<>M
2 F1,F2,F3,F4,F5 are common in both files
3 In case matching is not found records should be displayed in result as such it exits in File1 or File2

 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
awk '{FS="|"}{OFS="|"}
NR==1{for(i=1;i<NF;++i)n1=" na"n1}
NR==FNR{a1[$1]=substr($0,length($1+$2+$3+$4+$5)+1);t[$1];next}
FNR==1{for(i=1;i<NF;++i)n2=" na"n2}
{a2[$1]=substr($0,length($1+$2+$3+$4+$5)+1);t[$1];next}
END{for(i in t)print i (i in a1 ? a1 : n1) (i in a2 ? a2 : n2) | "sort"}
' file1.txt file2.txt >file3.txt


how can this scriot be modified to acheive the result
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top