Print merged rows from two files by applying if condition

Hi all,

I have list of two kind of files and I want to compare the rows and print the merged data by applying if condition.

First kind of file looks like:

and second kind of file looks like :

I want to print the rows present in second file followed by 3 more columns from first file by applying if condition

if (3rd column = 1st column - 1) #from second file
compare it in first file and
print 3rd, 4th and 5th column from the row having (column1 in frist file=
column1 in second file && 3rd column = 1st column +1 ) # in first file

and vive versa....
i.e, if (3rd column = 1st column + 1)
then print 3rd, 4th and 5th column from the row having (column1 in first file= column1 in second file && 3rd column = 1st column - 1 ) # in first file

therefore, the expected output must be like:

Thanks in advance !!!!!

I think your output is incorrect... Based on your conditions this is what is produced:

6 TRP 5 LEU 3.800394  7 GLN 3.837793
12 THR 13 ILE 3.787175  11 VAL 3.794017
98 ASN 97 LEU 3.807972  99 PHE 3.811863

And here is the code:

awk 'NR==FNR&&$3==($1-1){a[$1]=$0;next};NR==FNR&&$3==($1+1){b[$1]=$0;next};$3==($1+1)&&a[$1]{$0=a[$1]" "$3" "$4" "$5;print};$3==($1-1)&&b[$1]{$0=b[$1]" "$3" "$4" "$5;print};' file2 file1

Do I understand right:

  1. The keys for files are combinations of the second and forth fields (they are unique for each file).
  2. You need compare these fields.
  3. And these fields are always 3-letter words.

===

Well, I think after the bartus11's solution these questions are no longer needed.

Yes Yazu....u r correct

@ Bartus: yes, the answer indicated by u is correct....but, m sorry to say that I am not getting any output from ur help code and also I want to undersatnd ur code...can u please explain it to me and assist me to get the correct output

Can you post the sample data for which my code is not working?

Hey Bartus......

M really very sorry for the above said....as ur code is working on other similar files....and the output is exactly similar to what I needed....Thanks a lot :slight_smile:
my previous sample data file has some faults in column number....

Hi,

One more thing...... if it doesn't follow the condition $3=($1 +1) or $3=($1-1) in file2, then it must print the row as such from file2.

Im stuck in this....plz help me :wall: