I have . dat file which contains data in a specific format:
0 3 892 921 342
1 3 921 342 543
2 4 817 562 718 765
3 3 819 562 717 761
i need to compare each field in a row with another field of the same column but different row and cont the diffferences between the rows.
For ex: 892!=921 and 921!=342 and 342!=543 hence the (count of the differences between row 0 and row 1) = 3
Similarly i need to count the differences between the fields of row 1 and row 2
row 2 and row 3.. and soo on(not 0-1 , 2-3, 4-5...)
please can anyone body help me with an awk script?
I used NR.. but it is not pointing back to an already visited row
Thanks for the solution.. Using ur script, i am getting a wrong difference result when comparing with different length columns...
Can u pls explain the above script as i am new to awk
Diff between line [0] and line [1] is 3
Diff between line [1] and line [2] is 3
Diff between line [2] and line [3] is 3
Diff between line [3] and line [4] is 4
Here is mine:
cat infile
0 3 892 921 342
1 3 921 342 543
2 4 817 562 718 765
3 3 819 562 717 761
3 3 829
awk '
NR==1{split($0,a);c=NF;next}
{ s=(c>NF)?c-NF:"0";}
{ for (i=3;i<=NF;i++) if (a!=$i) b++ }
{print "Differences between Row", NR-1, "and Row",NR,")=",b+s;split($0,a);b=0;c=NF}
' infile
Differences between Row 1 and Row 2 )= 3
Differences between Row 2 and Row 3 )= 4
Differences between Row 3 and Row 4 )= 3
Differences between Row 4 and Row 5 )= 4