bash - How do I fix this code that should match fields between two files, and output various fields from each file? (awk) -
answered many before, i'm struggling somewhere here...
code:
awk '{ofs="\t"}nr==fnr{a[$1]=$1;a[$2]=$3;a[$3]=$4;next} $1 in a{print a[$1],a[$2],a[$3],$1,$3}' file1 file2 > output.txt file1:
spot dog black male felix cat white male file 2:
spot dog tall felix cat bad small output desired:
spot black male spot felix white male felix bad actual output:
spot black spot felix white felix bad a[$3] not being printed, or wasn't assigned properly...
you want more this
awk 'nr==fnr { # don't need value presence in array '$1 in a' check. a[$1]="" # value of $3 may not unique can't key '$1,3' since $1 unique. a[$1,3]=$3 # same here $3 above. a[$1,4]=$4 next } $1 in { print $1, a[$1,3], a[$1,4], $1, $3 }' file1 file2 the key here can use $1 unique (and unique-fying) key , have use relevant fields.
Comments
Post a Comment