Skip to main content
added 74 characters in body
Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141

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 delimiter
  • NR==FNR{} process only the first file
  • a[$1]=$2;next build an array a indexed by the first field with values of the second field then skip to the next row
  • a[$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 the next word 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

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 delimiter
  • NR==FNR{} process only the first file
  • a[$1]=$2;next build an array a indexed by the first field with values of the second field then skip to the next row
  • a[$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 the next word in the previous expression)
  • print $1":"$2":"a[$2] print everything as desired

After question edit:

awk -F: 'NR==FNR{a[$2]=$1;next}a[$1]{print a[$1]":"$1":"$2}' file1 file2

Output:

lisa:68468468468464:keyboard
bart:29482164591748:computer

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 delimiter
  • NR==FNR{} process only the first file
  • a[$1]=$2;next build an array a indexed by the first field with values of the second field then skip to the next row
  • a[$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 the next word in the previous expression)
  • print $1":"$2":"a[$2] print everything as desired

After question edit:

awk -F: 'NR==FNR{a[$1]=$2;next}a[$2]{print $1":"$2":"a[$2]}' file2 file1

Output:

bart:29482164591748:computer
 apu:29482164591748:computer
smithers:68468468468464:keyboard
lisa:68468468468464:keyboard
added 513 characters in body
Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141

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 delimiter
  • NR==FNR{} process only the first file
  • a[$1]=$2;next build an array a indexed by the first field with values of the second field then skip to the next row
  • a[$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 the next word in the previous expression)
  • print $1":"$2":"a[$2] print everything as desired

After question edit:

awk -F: 'NR==FNR{a[$2]=$1;next}a[$1]{print a[$1]":"$1":"$2}' file1 file2

Output:

lisa:68468468468464:keyboard
bart:29482164591748:computer

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

After question edit:

awk -F: 'NR==FNR{a[$2]=$1;next}a[$1]{print a[$1]":"$1":"$2}' file1 file2

Output:

lisa:68468468468464:keyboard
bart:29482164591748:computer

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 delimiter
  • NR==FNR{} process only the first file
  • a[$1]=$2;next build an array a indexed by the first field with values of the second field then skip to the next row
  • a[$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 the next word in the previous expression)
  • print $1":"$2":"a[$2] print everything as desired

After question edit:

awk -F: 'NR==FNR{a[$2]=$1;next}a[$1]{print a[$1]":"$1":"$2}' file1 file2

Output:

lisa:68468468468464:keyboard
bart:29482164591748:computer
added 195 characters in body
Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141

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

After question edit:

awk -F: 'NR==FNR{a[$2]=$1;next}a[$1]{print a[$1]":"$1":"$2}' file1 file2

Output:

lisa:68468468468464:keyboard
bart:29482164591748:computer

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

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

After question edit:

awk -F: 'NR==FNR{a[$2]=$1;next}a[$1]{print a[$1]":"$1":"$2}' file1 file2

Output:

lisa:68468468468464:keyboard
bart:29482164591748:computer
Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141
Loading