Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding numbers together

Status
Not open for further replies.

ZzapUK

Technical User
Mar 16, 2006
1
GB
I have a file with the contents:

6128 105
4356 15139
7052 1
5508 25
6932 200
5548 11468
5100 0
6128 105
4356 15125
7052 1
5508 25
6932 199
5548 11437
5100 0

Each of first number is repeated twice. For example there are 2 occurances of 6128, 4356, etc. What I want to do is subtract the second numbers for each first number. For example to achieve the output:

6128 0
4356 14
7052 0
5508 0
6932 1
5548 31
5100 0

Can anyone tell me how I might be able to do that with AWK?

Thanks!


 
feherke, if(v[$1]) evaluates to false if exists but is zero.

awk '
{if($1 in a)a[$1]-=$2;else a[$1]=$2}
END{for(i in a)print i,a}
' /path/to/input


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top