#!/bin/bash # Create a temp file to save the right column in so it can be easily searched with grep. right_file=$(mktemp) # Use awk to separate the columns. left=$(awk '{print $1}' input.txt) awk '{print $2}' input.txt > $right_file # Initialize the similarity score. similarity_score=0 for entry in $left; do # Count the number of times each ID from the left column occurs in the right column. count=$(cat $right_file | grep $entry | wc -l) # Multiply the count by the ID value, and add that the the cumulative sum. similarity_score=$((count * entry + similarity_score)) done echo $similarity_score # for l in $left; do # echo $l # done