Printing Columns of two files if content of first column in both file matches in Unix
You can do this with the join
command:
$ join -a 1 -a 2 -e NA -o '0,1.2,2.2' -t '|' -i <(sort f1) <(sort f2)AK|Victoria Street|Nilofer VillaBK|Admond Street|Homingo Apartmentck|NA|BluewatersDK|Business Street|NAname|address office|address home
Where:
-a 1
and-a 2
include the unjoined lines from either file.-e
and-o
work together to show the "NA" field. The man page doesn't mention this, but to use-e
you must specify-o
. We simply show the fields in this order: join column, second column from first file, second column from second file.-t
sets the delimiter
Of course we must also sort the files prior to using join (it's requisite), so we use process substitution. If your shell doesn't have that, you may use temporary files.
$ cat tst.awkBEGIN { FS=OFS="|" }{ name = (FNR>1 ? toupper($1) : $1) if (!seen[name]++) { names[++numNames] = name vals[name,1] = vals[name,2] = "NA" } vals[name,ARGIND] = $2}END { for (nameNr=1; nameNr<=numNames; nameNr++) { name = names[nameNr] print name, vals[name,1], vals[name,2] }}$ awk -f tst.awk file1 file2name|address office|address homeAK|Victoria Street|Nilofer VillaBK|Admond Street|Homingo ApartmentDK|Business Street|NACK|NA|Bluewaters
The above uses GNU awk for ARGIND, with other awks just add FNR==1{ARGIND++}
at the start of the script.