123 left right
456 up down
joe ding dong
jane flip flop
what I need to do is compare col1 and col2 in file1 with col1 in file2 and generate a new file that has lines like this:
joe 123 ding dong left right
jane 456 flip flop up down
what I did so far is this:
# this grabs the 'left' value
awk 'NR==FNR{a[$1]=$2;next} {print $1,$2,a[$2]}' file2 file1 >> file3
# this grabs the 'right' value
awk 'NR==FNR{a[$1]=$3;next} {print $1,$2,$3,a[$2]}' file2 file3 >> file4
which generates this line:
joe 123 left right
but I am stuck on grabbing joe's 'ding dong' and also, am I doing this right with multiple awk lines, or can it be made more compact ?
awk 'END {
for (i = 0; ++i <= FNR;)
if (i in r) print r
}
NR == FNR {
f[$1] = substr($0, index($0, $2))
next
}
$1 != $2 {
for (i = 0; ++i <= NF;)
$i in f && r[FNR] = (r[FNR] ? r[FNR] : $0) \
FS f[$i]
}' file2 file1
now I want to loop through both files and for each row I want to check if file1's 3th column equals file2's 4th column then append 5th column from file1 at the end of all the rows it matched in file2. example:
file1:
john doe 123 left right
file2:
address1 phone1 email1 123
address2 phone2 email2 123
output:
address1 phone1 email1 123 right
address2 phone2 email2 123 right
nawk 'NR==FNR{A[$3]=$5;next}{print $0 (($4 in A)?FS A[$4]:z)}' file1 file2
---------- Post updated at 07:27 PM ---------- Previous update was at 07:26 PM ----------
$ cat f1
john doe 123 left right
$ cat f2
address1 phone1 email1 123
address2 phone2 email2 123
$ nawk 'NR==FNR{A[$3]=$5;next}{print $0 (($4 in A)?FS A[$4]:z)}' f1 f2
address1 phone1 email1 123 right
address2 phone2 email2 123 right