0

I have two files stat_1.txt & stat_2.txt. As follows:

job_id | execution_time

--------+--------------------

12345 | 00:04:42.454282

12346 | 00:03:26.360487

====================

job_id | execution_time

--------+--------------------

12347 | 00:01:43.634521

12345 | 00:02:34.321451

There are of course thousands more records in each file. I need to arrange in a single file the job_id which are present in both files, for making a comparative analysis of execution times. (similar to what we might do in a table join.) How do I do this? I'm sure there must be an elegant way in bash.

The files are not ordered by job_id. There are some job_id which are present in only one file but not the other. (Sorry if the tables above look sloppy. I'm not familiar with the formatting, and I'm in bit of a hurry.)

0

You can do it like that

awk 'NR==FNR{a[$1$2]=$3;next}
{k=$1$2;print k,a[k],$3;delete a[k]}
END{for(k in a)print k,a[k]}' test1 test2

test1 and test2 are the two files to combined

Here is the example :

[xxxx@xxxx test]$ awk 'NR==FNR{a[$1$2]=$3;next}
                  {k=$1$2;print k,a[k],$3;delete a[k]}
                  END{for(k in a)print k,a[k]}' test1 test2

job_id | execution_time execution_time
--------+--------------------
12347 |  00:01:43.634521
12345 | 00:04:42.454282 00:02:34.321451
12346 | 00:03:26.360487

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.