“The COBOL way” of doing this would be to SORT the individual files by the same key, then write logic that MERGEs the two sorted files together. This is what you are doing here.
Another way to do it would be ... to use DBD::SQLite. SQLite is a public domain database engine which works with single files. (There is no “server.”) So, instead of storing your data (initially, perhaps) as “a flat file,” you store it as a database... this “database” being, in fact, “just a file.” SQLite is extremely robust and fast, and it is very heavily used in smart phones and so-forth. I find myself using it rather constantly in situations where I normally would have used “a flat file.” The overhead of doing so is, “essentially, nil.”