user11145522
user11145522

Reputation:

Merge Two Text Files Matching Values

I'm not 100% this can be done with AWK, if there is a better way than I wouldn't mind using that method.

I have two text files:

One file looks like this:

email:phone#:firstname

The second file looks like this:

phone#:lastname

Now I know that these two files come from the same source, but they were split up at one point. Technically I could merge them back together by taking the phone# from the second file and matching it with the phone# in the first file. Once it finds a match then it can take lastname and append it to the appropriate line on the first file. The end result would be

email:phone#:firstname:lastname

What would be the best way to do something like this?

Upvotes: 0

Views: 63

Answers (1)

karakfa
karakfa

Reputation: 67467

with join

$ join -t: -12 -21 -o1.1,1.2,1.3,2.2 <(sort -t: -k2 file1) <(sort file2)

Upvotes: 3

Related Questions