AWK passing output, line by line to variable..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tarantulus
    New Member
    • May 2007
    • 114

    AWK passing output, line by line to variable..

    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,$val ue"

    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
  • radoulov
    New Member
    • Jun 2007
    • 34

    #2
    Please provide sample input and example of the desired output.

    Comment

    • Tarantulus
      New Member
      • May 2007
      • 114

      #3
      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

      Comment

      • radoulov
        New Member
        • Jun 2007
        • 34

        #4
        It the date column is sorted:
        (use nawk or /usr/xpg4/bin/awk on Solaris)

        Code:
        awk -F, 'END { print d, t }
        !_[$1]++ && d { print d, t; d = t = "" }
        { d = $1; t += $2 }' OFS=\\= filename

        Comment

        • Tarantulus
          New Member
          • May 2007
          • 114

          #5
          Thanks a lot,

          I actually worked it out myself (in a much more convoluted way) but your way is much prettier :)

          Comment

          Working...