Hello,
I have two files containing both two columns:
$ cat file1
col1 nbr1
col2 nbr2
col3 nbr3
...
$ cat file2
val1 ri1
val2 ri2
val3 ri3
...
I need to compare every nbr (nbr1, nbr2,...) in the second column of file1 with every ri (ri1, ri2,...) in the second column of file2. And when nbr=ri ===> write in the output file : [(val - col) nbr]
The output file shoud look like :
$ cat file3
(val1 - col1) nbr1 (if nbr1=ri1)
(val3 - col3) nbr3 (if nbr3=ri3)
Thank you,
Does this work for you?
paste file1 file2 | awk '$2==$4{print "("$3" - "$1") "$2}' > file3
Thank you for your reply, but it does not fullfil the need.
Here is my exp :
$ cat file1
3 22
4 33
6 77
$
$ cat file2
1 22
5 77
$
$ cat file3
(3 - 1) 22
$
But i need the the output file to be like :
$ cat file3
2 22 =========> (3 - 1) = 2
1 77 =========> (6 - 5) = 1
Can we do it with loop :
for i in file1
for j in file2
if (i==j)
echo ...
?
Thank you
---------- Post updated at 01:19 AM ---------- Previous update was at 12:13 AM ----------
Can you please help me with my question as it's quit urgent ?
Thank you
$ awk 'NR==FNR{a[$2]=$1;next} {print a[$2]-$1,$2}' file1 file2
2 22
1 77
the command did 90% of the job, but it displays some lines that are in file2 but not present in file1.
$ cat file1
10 1111
20 1112
30 1113
40 1114
60 1116
$ cat file2
2 1111
4 1112
7 1114
5 1115
$ awk 'NR==FNR{a[$2]=$1;next} {print a[$2]-$1,$2}' file1 file2
8 1111
16 1112
33 1114
-5 1115
The last line of the result exists in file2 but not does not exist in file1.
The result i suspect is :
$ awk 'NR==FNR{a[$2]=$1;next} {print a[$2]-$1,$2}' file1 file2
8 1111
16 1112
33 1114
Can you please correct it ?
awk 'NR==FNR{a[$2]=$1;next}
a[$2]!="" {print a[$2]-$1,$2}' file1 file2