1

I have a file that looks something like this

InvoiceDate    Bill Person     BAN    Type    Amount    Amount-Expenses   GreenFee      TaxAmount    FinalTotal
1524753593    Jennifer Doe     39193  S       33.30      20               2             1            75
1524753593    Jason Bourne Doe 395931 S       27.00      11               2             1            57.33
1524753593               Feature    D    15    12.00    64.20
1524753593    Jean-Pierre Doe  395931 S       53.10      44               2             1            12.33
1524753593               Feature    D    12    5.00     74.32
1524753593    Jamie Doe        39193  S       35.30      42               2             1            53.11

I need to get the total for the file...it has several thousand entries like the sampling above. I need the total for the last column but iff the number of columns is greater than 8.

I thought about using grep to get only the columns I need into a temporary file but give that the file can easily be osscillate between 30 to 100+ MB I believe it'd be faster not to involve an intermediate file. I thought about reverseing the file grabbing all the numbers from the first column where the type is S but I wasn't sure exactly how to do that via awk.

3
awk 'NF>=8 && NR>1 { entries++; sum+=$8 } END { print "Total for column 8:", sum, "over", entries, "records with this value present." }' inputfile
  • Awesome thanks. – Mark D Mar 18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.