Osama Ahmed
Osama Ahmed

Reputation: 69

compare between two columns and subtract them

my question

i have one file

344  0
465  1
729  2
777  3
676  4
862  5

766  0
937  1
980  2
837  3
936  5

i need to compare each two pair (zero with zero, one with one and so on) if the value exist(any value of column two should exist two times) subtract 766-344 , 937-465 and so on if not exist like the forth value do nothing (4 exist one time so do nothing) the output

422
472
251
060
074

also i need to add index example

1 422
2 472
3 251
4 060
5 074

finally i need to add this code as part of tcl script, or function of tcl porgram

I have a tcl script contain awk functions like this

  set awkCBR0 {
    {
    if ($1 == "r" && $6 == 280) {
    print $2, i  >> "cbr0.q";
             i +=1 ;
           }
    }
    }

    exec rm -f cbr0.q 
    exec touch cbr0.q

exec awk $awkCBR0 cbr.trq

thanks

Upvotes: 1

Views: 162

Answers (1)

jaypal singh
jaypal singh

Reputation: 77085

Try this:

awk 'a[$2]{printf "%d %03d\n",++x,$1-a[$2];next}{a[$2]=$1}' file

Output

$ awk 'a[$2]{printf "%d %03d\n",++x,$1-a[$2];next}{a[$2]=$1}' file
1 422
2 472
3 251
4 060
5 074

I will leave it for you to add it to tcl function.

Upvotes: 1

Related Questions