neversaint neversaint - 5 months ago 16
Linux Question

Joining multiple fields in text files on Unix

How can I do it?

I have a file that looks like this

foo 1 scaf 3
bar 2 scaf 3.3


File2 looks like this

foo 1 scaf 4.5
foo 1 boo 2.3
bar 2 scaf 1.00


What I want to do is to fine lines that co-occur in file1 and file2
when field 1,2,3 are the same.

Is there a way to do it?

Answer

you can try this

awk '{
 o1=$1;o2=$2;o3=$3
 $1=$2=$3="";gsub(" +","")
 _[o1 FS o2 FS o3]=_[o1 FS o2 FS o3] FS $0
}
END{ for(i in _) print i,_[i] }' file1 file2

output

$ ./shell.sh
foo 1 scaf  3 4.5
bar 2 scaf  3.3 1.00
foo 1 boo  2.3

If you want to omit uncommon lines

awk 'FNR==NR{
 s=""
 for(i=4;i<=NF;i++){ s=s FS $i }
 _[$1$2$3] = s
 next
}
{
  printf $1 FS $2 FS $3 FS
  for(o=4;o<NF;o++){
   printf $i" "
  }
  printf $NF FS _[$1$2$3]"\n"
 } ' file2 file1

output

$ ./shell.sh
foo 1 scaf 3  4.5
bar 2 scaf 3.3  1.00