if columns from Input_file are present in query file, print "YES" along with columns in table in file_name as header

Viewed 49

Input_file:

15 47123466 rs98765576 C T* 100 PASS . GT 0|0 0|0 0|0 0|0 0|0 0|0 0|0

file1:

15 47123466 rs98765576 C T

file2:

11 123345 rs9173547 A G

Output:

#IP_CHR   IP_POS  IP_ID   IP_REF  IP_ALT  OP_CHR  OP_POS  OP_ID   OP_REF  OP_ALT  OP_QUAL    OP_FILTER  OP_INFO file1_name file2_name
15 47123466 rs98765576 C T C T* 100 PASS . GT 0|0 0|0 0|0 0|0 0|0 0|0 0|0 YES NO

if entries from input file (column1,2,3,4) are present in file1 & file2, then output should have input_file(column1,2,3,4) and file1 & file2 names as header and "YES/No" below to file_name headers

I tried this

awk 'NR==FNR{firstfile[$1,$2,$4];next} ($1,$2,$4) in firstfile { print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"FILENAME }'
giving output as 15 47123466 rs98765576 C T C T* 100 PASS . GT 0|0 0|0 0|0 0|0 0|0 0|0 0|0 file1_name
1 Answers

With your shown samples, attempts; please try following awk program.

awk '
BEGIN{
  s1="#IP_CHR   IP_POS  IP_ID   IP_REF  IP_ALT  OP_CHR  OP_POS  OP_ID   OP_REF  OP_ALT  OP_QUAL    OP_FILTER  OP_INFO "
}
{
  key=($1 FS $2 FS $3 FS $4)
}
ARGIND<3{
  arr[key]
  file=(file?file OFS:"")FILENAME
  next
}
FNR==1{
  print s1,file
}
{
  val=""
  for(j in arr){
    val=(val?val OFS:"")(key==j?"YES":"NO")
  }
  print $0,val
}
' file1 file2 input_file
Related