Consider two csv files:$ cat given.csv123,John246,Paul369,George987,Ringo$ cat family.csv246,McCartney123,Lennon987,Starr369,HarrisonNote 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,Lennon246,Paul,McCartney369,George,Harrison987,Ringo,StarrI 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.
Match and combine records in Bash
bash;text processing;awk;grep
With join and sort:join -t , <(sort given.csv) <(sort family.csv)Output:123,John,Lennon246,Paul,McCartney369,George,Harrison987,Ringo,StarrWith grep:#!/bin/bashF1=given.csvF2=family.csvD=,           # delimiterwhile IFS=$D read FIRST_COLUMN REST; do  T=$FIRST_COLUMN$D$REST  T+=$(grep -oP ^$FIRST_COLUMN\K$D.* $F2) && echo $Tdone < $F1Output:123,John,Lennon246,Paul,McCartney369,George,Harrison987,Ringo,Starr