With awk:
awk -F: 'NR==FNR{a[$1]=$2;next}a[$2]{print $1":"$2":"a[$2]}' file1 file2 Output:
bart:29482164591748:computer smithers:68468468468464:keyboard lisa:68468468468464:keyboard Explanation:
awk -F:start awk treating colon as a fields delimiterNR==FNR{}process only the first filea[$1]=$2;nextbuild an arrayaindexed by the first field with values of the second field then skip to the next rowa[$2]{}process only if value of previously build array with the index of the current second field is not empty (this is done only for the file2, because of thenextword in the previous expression)print $1":"$2":"a[$2]print everything as desired
After question edit:
awk -F: 'NR==FNR{a[$2]=$1;nexta[$1]=$2;next}a[$1]a[$2]{print a[$1]"$1":"$1""$2":"$2"a[$2]}' file1 file2 file1 Output:
lisabart:6846846846846429482164591748:keyboardcomputer bart apu:29482164591748:computer smithers:68468468468464:keyboard lisa:68468468468464:keyboard