I want to merge these two files into one. The desired result from the above would be:
Blueberry blue 14 tasty 12 78
Raspberry red 12 - - -
Blackberry dark 4 sour 4 3
Strawberry - - yummy 33 88
So the three columns of file1.txt and the four columns of file2.txt should result in a new file with six columns where all berries present in these two files only are represented with one row each. If a berry is not present in one of the two files, the corresponding entries from that file are replaced with a dash (like for Strawberry and Raspberry above).
Thanks RudiC! Just get a minor error (also added one missing dash in your command):
$ awk 'FNR==NR{a[$1]=$2 FS $3 FS $4;next}{ print $0, a[$1]?a[$1]:"- - -"; delete a[$1]} END {for (i in a) print i, "- -", a}' file2.txt file1.txt
Blueberry blue 14 tasty 12 78
Raspberry red 12 - - -
Blackberry dark 4 sour 4 3
- - -
Strawberry - - yummy 33 88
So there's an extra "- - -" there that I can't explain. Bonus question: if I don't know how many columns file1 and file2 have, is there some way of making this command more dynamic?
awk '
FNR==NR {a[$1]=$2
d="-"
for (i=3; i<=NF; i++) {a[$1]=a[$1] FS $i; d=d FS "-"}
next
}
{print $0, a[$1]?a[$1]:d
D="-"
for (i=3; i<=NF; i++) D=D FS "-"
delete a[$1]
}
END {for (i in a) print i, D, a
}
' file2 file1