[How To] Joining unrelated files in bash

So, a quick bash tip to whom it may concern, specially future Daniel.

Suppose you have two files that you know are in the same order. The first line in the first file correspond to the first line in the second, the second line to the second line and so on. For example, what motivated me to write this is a list of pods from CocoaPods and some stats from each pod, but the stats don’t have the name of the pod.

The list of pods is like this:

Enumerable
Torus-fetchNodeDetails
CXDownloader
XXLoginModule
YLImagePickerController
GTCategories
SNNextResponder
GSNetworking
OTMacro
ZFTableViewSnapshot

And the stats like this:

{"github":{"subscribers":2,"stargazers":18,"f...
{"github":{"subscribers":null,"stargazers":nu...
{"github":{"subscribers":1,"stargazers":0,"fo...
{"github":{"subscribers":1,"stargazers":0,"fo...
{"github":null,"cocoadocs":{"install_size":nu...
{"github":{"subscribers":1,"stargazers":0,"fo...
{"github":{"subscribers":2,"stargazers":21,"f...

What I want is to have in each line the name of the pod and the stats. Usually such task is done with join, but join requires some shared key, like and id or number. But here we don’t any form of key. However, since we know that line one in the pods corresponds to line one in the stats, we can use cat -n to enumerate each line and effectively add the needed key:

$ join <(cat -n pods.txt) <(cat -n pod_stats.jsonl) | head
1 Enumerable {"github":{"subscribers":2,"stargazers":18,"forks":2...
2 Torus-fetchNodeDetails {"github":null,"cocoadocs":{"install_siz...
3 CXDownloader {"github":{"subscribers":1,"stargazers":0,"forks":...
4 XXLoginModule {"github":{"subscribers":null,"stargazers":null,"...
5 YLImagePickerController {"github":{"subscribers":3,"stargazers"...
6 GTCategories {"github":{"subscribers":1,"stargazers":0,"forks":...
7 SNNextResponder {"github":{"subscribers":1,"stargazers":0,"fork...
8 GSNetworking {"github":null,"cocoadocs":{"install_size":null,"t...
9 OTMacro {"github":{"subscribers":1,"stargazers":0,"forks":0,"co...
10 ZFTableViewSnapshot {"github":{"subscribers":2,"stargazers":21...

Now that we connected each pod name to its stats we can do some more bash magic to leave it the format we want. For example we can use awk to build a new json with this new information. Imagination is the limit here.