I have 2 files containing list. Column 1 is userIds & column 2 is associated values
# cat file1 e3001 75 n5244 30 w1453 500 #cat file2 d1128 30 w1453 515 n5244 30 e3001 55 Things to consider.
- userIds may not be sorted exactly in both files
- Number of userIds may vary in files
REQUIRED
- firstly, userId from file1:column1 must match UserId in file2:column1
- next compare their values in file1:column2 with file2:column2
- print where values has variance. also extra userIds if any
OUTPUT:
e3001 has differnece, file1 value: 75 & file2 value: 55 w1453 has differnece, file1 value: 500 & file2 value: 515 d1128 is only present in filename: file1|file2 solution with 1liner-awk or bash loop is welcome
I'm trying to loop, but it's spitting garbage, guess there's some mislogic
#!/usr/bin/env bash ## VARIABLES FILE1=file1 FILE2=file2 USERID1=(`awk -F'\t' '{ print $1 }' ${FILE1}`) USERID2=(`awk -F'\t' '{ print $1 }' ${FILE2}`) USERDON1=(`awk -F'\t' '{ print $2 }' ${FILE1}`) USERDON2=(`awk -F'\t' '{ print $2 }' ${FILE2}`) for user in ${USERID1[@]} do for (( i = 0; i < "${#USERID2[@]}"; i++ )) #for user in ${USERID2[@]} do if [[ ${USERID1[$user]} == ${USERID2[i]} ]] then echo ${USERID1[$user]} MATCHES BALANCE FROM ${FILE1}: ${USERDON1[$i]} WITH BALANCE FROM ${FILE2}: ${USERDON2[$i]} else echo ${USERID1[$user]} fi done done Below is copied file right from linux box. It's tab separated, but awk works with tab also, as far as I know.
#cat file1 e3001 55 n5244 30 w1453 515