Artificial intelligent assistant

Match and combine records in Bash Consider two csv files: $ cat given.csv 123,John 246,Paul 369,George 987,Ringo $ cat family.csv 246,McCartney 123,Lennon 987,Starr 369,Harrison _Note that the lines are not ordered!_ How might I use `awk`, `grep`, and other `bash` tools to get a combined output file (order unimportant): 123,John,Lennon 246,Paul,McCartney 369,George,Harrison 987,Ringo,Starr I was thinking about running a `for` loop over the first file, then using `awk` to get the id and _then_ grepping the second file for the relevant record. **Is there a more straightforward way to do this in Bash**? I find that often bash has a clever, efficient way to handle text files that I'm not yet familiar with.

**With join and sort:**


join -t , <(sort given.csv) <(sort family.csv)


Output:


123,John,Lennon
246,Paul,McCartney
369,George,Harrison
987,Ringo,Starr


* * *

**With grep:**


#!/bin/bash

F1="given.csv"
F2="family.csv"
D="," # delimiter

while IFS="$D" read FIRST_COLUMN REST; do
T="$FIRST_COLUMN$D$REST"
T+="$(grep -oP "^$FIRST_COLUMN\K$D.*" "$F2")" && echo "$T"
done < "$F1"


Output:


123,John,Lennon
246,Paul,McCartney
369,George,Harrison
987,Ringo,Starr

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy a20cc410af25eea77a90b80c09fb2675