To find and compare the data

hi ,

from the awk command i'm getting the output where the 2nd column of both are match in row wise,
if it present in that same column some where else, I cannot get the correct output.
awk 'FNR==NR {a[$1]=$5;next} $1 in a {if ($5==a[$1]) t=0; else {t=$5-a[$1]};print $0,t}' file.tsv file2.tsv

please check the thread Compare two numbers which present in two different files

---------- Post updated at 07:50 PM ---------- Previous update was at 07:40 PM ----------

col1 col2 col3
11 15465 13.60
12 24656 11.00
13 245896 754.00
col1 col2 col3
11 245896 754.00
12 15465 13.60
13 24656 11.00

this will give the wrong output

I recognize that for some people here, english is not the primary language. I do not think you explained this well.

  1. show input file

  2. show your command

  3. show what you are getting

  4. show what you want to get for output.

Hi,

here i have given the input files and output files, and awk command
awk 'FNR==NR {a[$1]=$4;next} $1 in a {if ($4==a[$1]) t=0; else {t=$4-a[$1]};print $0,t}' file1.tsv file2.tsv

i have to find the COL1 number in file2 and get the difference of the matched number's COL3 Value that is i have to find the 14150524 in file2 and take a difference of 39.88 - 37.88 and the result i want is like 10527 14150524 Sa 39.88 -2

File 1

SR.no	COL1	COL2	COL3
10527	14150524	Sa	39.88
10528	12311440	Sa	0
10529	12441731	Sa	111.66
10530	15120599	Sa	69.97
10531	21635123	Sa	149.99
10532	9854892	Sa	27.53
10533	14526541	Sa	67.06
10534	10993779	Sa	99
10535	15684120	Sa	112.99
10536	6051457	Sa	249
10537	10983989	Sa	149.97
10538	8222030	Sa	59
10539	10910428	Sa	237.2
10540	8477371	Sa	126.44

File2

SR.no	COL1	COL2	COL3
10536	6051457	Sb	249
10537	10983989	Sb	149.97
10538	8222030	Sb	59
10530	15120599	Sb	69.97
10531	21635123	Sb	149.99
10532	8477371	Sb	126.44
10533	14526541	Sb	67.09
10534	10993779	Sb	99
10535	15684120	Sb	112.99
10527	14150524	Sb	37.88
10528	12311440	Sb	0
10529	12441731	Sb	111.66
10539	10910428	Sb	237.2
10540	9854892	Sb	27.54

Result

SR.no	COL1	COL2	COL3	COL4
10527	14150524	Sa	39.88	0
10528	12311440	Sa	0	0
10529	12441731	Sa	111.66	0
10530	15120599	Sa	69.97	0
10531	21635123	Sa	149.99	0
10532	9854892	Sa	27.53	-98.91
10533	14526541	Sa	67.06	-0.03
10534	10993779	Sa	99	0
10535	15684120	Sa	112.99	0
10536	6051457	Sa	249	0
10537	10983989	Sa	149.97	0
10538	8222030	Sa	59	0
10539	10910428	Sa	237.2	0
10540	8477371	Sa	126.44	98.9

Assuming you want result -2 you may want t=a[$1]-$4 (scanning file2 at first)

awk 'FNR==NR{a[$1]=$4;next}$1 in a{t=a[$1]-$4;print $0,(FNR==1)?"":t}' file2 file1

---------- Post updated at 12:08 PM ---------- Previous update was at 11:53 AM ----------

with a bit of formating

awk 'FNR==NR{a[$1]=$4;next}$1 in a{t=a[$1]-$4;printf"%-8s %8s %-4s %8s %8s\n",$1,$2,$3,$4,(FNR==1)?"COL4":t}' file2 file1
$ awk 'FNR==NR{a[$1]=$4;next}$1 in a{t=a[$1]-$4;printf"%-8s %8s %-4s %8s %8s\n",$1,$2,$3,$4,(FNR==1)?"COL4":t}' f2 f1
SR.no        COL1 COL2     COL3     COL4
10527    14150524 Sa      39.88       -2
10528    12311440 Sa          0        0
10529    12441731 Sa     111.66        0
10530    15120599 Sa      69.97        0
10531    21635123 Sa     149.99        0
10532     9854892 Sa      27.53    98.91
10533    14526541 Sa      67.06     0.03
10534    10993779 Sa         99        0
10535    15684120 Sa     112.99        0
10536     6051457 Sa        249        0
10537    10983989 Sa     149.97        0
10538     8222030 Sa         59        0
10539    10910428 Sa      237.2        0
10540     8477371 Sa     126.44    -98.9