By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,696 Members | 1,396 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,696 IT Pros & Developers. It's quick & easy.

AWK passing output, line by line to variable..

Tarantulus
100+
P: 114
Hi guys,

ok this is a tough one to explain, but I'll try my best:

I have a CSV file in the format "$date,$value"

what I want to do, is work through the file line by line (I guess piping through AWK to get $date and $value), and while $date does not change add $value to a running total $total$i

when $date changes $i increments so $total$i is a new variable ($total1 becomes $total2)

does this make sense and is it possible?

thanks in advance
Jun 11 '08 #1
Share this Question
Share on Google+
4 Replies

radoulov
P: 34
Please provide sample input and example of the desired output.
Jun 11 '08 #2

Tarantulus
100+
P: 114
sample input:

11/01/2008,312
11/01/2008,200
12/01/2008,600

sample output:

total1 = 512
total2 = 600

OR

11/01/2008 - 512
12/01/2008 - 600

Thanks
Jun 11 '08 #3

radoulov
P: 34
It the date column is sorted:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Expand|Select|Wrap|Line Numbers
  1. awk -F, 'END { print d, t }
  2. !_[$1]++ && d { print d, t; d = t = "" }
  3. { d = $1; t += $2 }' OFS=\\= filename
Jun 12 '08 #4

Tarantulus
100+
P: 114
Thanks a lot,

I actually worked it out myself (in a much more convoluted way) but your way is much prettier :)
Jun 12 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.