I am new in awk and shell, wondering how can I merge 2 files with the line that have the same record using shell/awk? file1 and file2 may have different order for the Name. I only want to merge the lines that has the same record. please help.
file1.txt
Mary 68
Tom 50
Jason 45
Lu 66
file2.txt
Jason 37
Tom 26
Mary 74
Tina 80
mergefile.txt
Marry 68 74
Tom 50 26
Jason 45 37
I have a try of awk, but it takes some times to running the script. Wondering if there could be a more faster and simple implement.
cat file1.txt | while read line
do
score1=$( echo $line | awk '{print $2}');
name1=$( echo $line | awk '{print $1}');
cat file2.txt | while read l
do
score2=$( echo $l | awk '{print $2}');
name2=$( echo $l | awk '{print $1}');
if [[ $name1 == $name2 ]]
then
echo "$name1 $score1 $score2" >> mergefile
break
fi
done
done
l
as it looks far too much like the number1
and so obfuscates your code. – Ed Morton Mar 07 '20 at 18:07