lundi 21 janvier 2019

replace pattern in file 2 with pattern in file 1 if contingency is met

I have two tab delimted data files the file1 looks like:

cluster_j_72    cluster-32  cluster-32  cluster_j_72
cluster_j_75    cluster-33  cluster-33  cluster_j_73
cluster_j_8 cluster-68  cluster-68  cluster_j_8

the file2 looks like:

   NODE_148        67545   97045   cluster-32
   NODE_221        1       42205   cluster-33
   NODE_168        1       24506   cluster-68

I would like to confirm that, for a given row, in file1 columns 2 and 3; as well as 1 and 4 are identical. If this is the case then I would like to take the value for that row from column 2 (file 1) find it in file2 and replace it with the value from column 1 (file 1). Thus the new output of file 2 would look like this (note because column 1 and 4 dont match for cluster 33 (file1) the pattern is not replaced in file2):

   NODE_148        67545   97045   cluster_j_72  
   NODE_221        1       42205   cluster-33  
   NODE_168        1       24506   cluster_j_8  

I have been able to get the contingency correct (here printing the value from file1 i'd like to use to replace a value in file2):

    awk '{if($2==$3 && $1==$4){print $1}}'file1

If I could get sed to draw values ($2 and $1) from file1 while looking in file 2 this would work:

     sed 's/$2(from file1)/$1(from file1)/' file2

But I don't seem to be able to nest this sed in the previous awk statement, nor get sed to look for a pattern originating in a different file than it's looking in.

thanks!

Aucun commentaire:

Enregistrer un commentaire