INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Merging and Sorting Files

Merging and Sorting Files

(OP)
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" = "http://downloadurl/502013341_BQ_cnt_1_r480P.mp4";
"LANG_AJ" = "http://downloadurl/502013341_AJ_cnt_1_r480P.mp4";
"LANG_BS" = "http://downloadurl/502013341_BS_cnt_1_r480P.mp4”;

Final File:
"LANG_AJ" = "azerbejdżański (alfabet grażdanka)";
"LANG_AJ" = "http://downloadurl/502013341_AJ_cnt_1_r480P.mp4";
"LANG_BS" = "basa (Kamerun)";
"LANG_BS" = "http://downloadurl/502013341_BS_cnt_1_r480P.mp4”;
"LANG_BQ" = "baskijski";
"LANG_BQ" = "http://downloadurl/502013341_BQ_cnt_1_r480P.mp4";

RE: Merging and Sorting Files

(OP)
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

RE: Merging and Sorting Files

(OP)
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

RE: Merging and Sorting Files

Quote (JBorges)

. . . 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

RE: Merging and Sorting Files

(OP)
@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" = "http://downloadurl/502013341_BQ_cnt_1_r480P.mp4";
"LANG_AJ" = "http://downloadurl/502013341_AJ_cnt_1_r480P.mp4";
"LANG_BS" = "http://downloadurl/502013341_BS_cnt_1_r480P.mp4”;

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" = "http://downloadurl/502013341_AJ_cnt_1_r480P.mp4";
"LANG_AJ" = "502013341_AJ_cnt_1_r480P.mp4";
"LANG_BS" = "basa (Kamerun)";
"LANG_BS" = "http://downloadurl/502013341_BS_cnt_1_r480P.mp4”;
"LANG_BS" = "502013341_BS_cnt_1_r480P.mp4”;
"LANG_BQ" = "baskijski";
"LANG_BQ" = "http://downloadurl/502013341_BQ_cnt_1_r480P.mp4";
"LANG_BQ" = "502013341_BQ_cnt_1_r480P.mp4";

RE: Merging and Sorting Files

(OP)
Thanks PHV. Works like a charms :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close