collecting data from file

Jeremy Yallop jeremy at jdyallop.freeserve.co.uk
Fri Apr 11 11:36:58 CEST 2003


Mustafa Celik wrote:
> I want to scroll thru a file;
>    * find lines that match a string (e.g. HELLO) on the 2nd column
>    * add up the 4th column (an integer) on each matching line, say the 
> variable is TOTAL
>    * subtract the 4th column from TOTAL if another is string (e.g. BYE) 
> is hit on 2nd column of a line

I'd use awk for this.

  { 
    if ($2 == "HELLO")    { total += $4 }
    else if ($2 == "BYE") { total -= $4 }
  }
  END { print total }

Jeremy.




More information about the Python-list mailing list