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

Merging and Sorting Files 1

Status
Not open for further replies.

JBorges

Technical User
Jan 12, 2009
57
DK
Hello.

I'm looking for a way to merge 2 files, and have the final file sorted according to how the list is set up in fileA.txt, the master file, like described below. Can it be done with AWK?

fileA.txt:
"LANG_AJ" = "azerbejdżański (alfabet grażdanka)";
"LANG_BS" = "basa (Kamerun)";
"LANG_BQ" = "baskijski";

fileB.txt:
"LANG_BQ" = ""LANG_AJ" = ""LANG_BS" = "
Final File:
"LANG_AJ" = "azerbejdżański (alfabet grażdanka)";
"LANG_AJ" = ""LANG_BS" = "basa (Kamerun)";
"LANG_BS" = ""LANG_BQ" = "baskijski";
"LANG_BQ" = "
 
What about this ?
Code:
awk 'NR==FNR{t[$1]=$0;next}{print $0"\n"t[$1]}' fileB.txt fileA.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yep, PHV! Thanks a million. It works! It doesn't sort the final list alphabetically, but according to the list of fileA.txt

This helps me avoid having to do this manually!

Could you explain the awk code?

Philip
 
Hi PHV.

Just returning briefly to this topic.

What if I have more than 2 files that need to be merged using this awk command. I tried the below, but that will always only take 2 of the 3 files.

awk 'NR==FNR{t[$1]=$0;next}{print $0"\n"t[$1]}' fileC.txt fileB.txt fileA.txt
 
JBorges said:
. . . What if I have more than 2 files that need to be merged using this awk command. I tried . . . .

Well Mr J, you just figured out that providing limited requirements will result in limited functionality in the solution.

Perhaps the next question(s) would be:

"When you have more than 2 files, in which order within the same key group will the result set be sorted on?"

"When you have records which do not match the key in fileA.txt, in what order are they to be included in the sorted result?"

. . . E t c . . .
[3eyes]







----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
@LKBrwnDBA

Good point. Here's the additional information:

The 3rd file, fileC.txt, contains the same keys. So how can I adopt the awk code in the second post to accommodate 3 or more files with the same keys, "LANG_XX" etc.?

fileA.txt:
"LANG_AJ" = "azerbejdżański (alfabet grażdanka)";
"LANG_BS" = "basa (Kamerun)";
"LANG_BQ" = "baskijski";

fileB.txt:
"LANG_BQ" = ""LANG_AJ" = ""LANG_BS" = "
fileC.txt:
"LANG_BQ" = "502013341_BQ_cnt_1_r480P.mp4";
"LANG_AJ" = "502013341_AJ_cnt_1_r480P.mp4";
"LANG_BS" = "502013341_BS_cnt_1_r480P.mp4”;

Final File:
"LANG_AJ" = "azerbejdżański (alfabet grażdanka)";
"LANG_AJ" = ""LANG_AJ" = "502013341_AJ_cnt_1_r480P.mp4";
"LANG_BS" = "basa (Kamerun)";
"LANG_BS" = ""LANG_BS" = "502013341_BS_cnt_1_r480P.mp4”;
"LANG_BQ" = "baskijski";
"LANG_BQ" = ""LANG_BQ" = "502013341_BQ_cnt_1_r480P.mp4";
 
So, use another way:
Code:
awk 'NR==FNR{a[NR]=$1;t[$1]=$0;next}{t[$1]=t[$1]"\n"$0}END{for(i=1;i in a;++i)print t[a[i]]}' fileA.txt fileB.txt fileC.txt ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top