#!/bin/bash MergedFile=$1 DefaultFile=$2 ProjectFile=$3 gawk 'BEGIN{FS=OFS=",";Pass=0; count=0;} Pass==1 && NF==4{records[$1]=$0; count++; order[count]=$1;} Pass==2 && NF==4{records[$1]=$0;} END{for (i=1; i<=count; i++) print records[order[i]];}' Pass=1 $DefaultFile Pass=2 $ProjectFile | tee $MergedFile;