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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need script ideas 2

Status
Not open for further replies.

grepper

Technical User
Jun 4, 2003
49
US
I have a list generated by a SQL script. The list is a date and number per line.

Ex: 10-02-04,3234
10-03-04,280
10-04-04,9843

The problem is I need something to compare the date from line 1 to line 2 and if there are the same add the number from line 2 to line 1. The end result should be no duplicate dates and all numbers totaled for each date. I'm sure there is an easy fix I just can't think of it right now. Thanks is advance....
 
Brute force method:
awk -F, '{t[$1]+=$2}END{for(d in t)printf "%s,%d\n",d,t[d]' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
[tt]
awk '
BEGIN { FS = OFS = "," }
1==NR { date = $1; sum = $2; next }
{ if ( date != $1 )
{ print date, sum
sum = 0
}
date = $1
sum += $2
}
END { print date, sum }
' infile >outfile
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top