NR %3 == 1 { split($0, arr, "") for (a in arr) { A[arr[a]]=1 } } NR %3 == 2 { split($0, arr, "") for (a in arr) { B[arr[a]]=1 } } NR %3 == 0 { split($0, arr, "") for (a in arr) { c = arr[a] if ((c in A) && (c in B)) { comm = c } } i = index("abcdefghijklmnopqrstuvwxyz", comm) j = index("ABCDEFGHIJKLMNOPQRSTUVWXYZ", comm)+26 S += i > 0 ? i : j delete A delete B } END { print S}