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

subtotal with awk

Status
Not open for further replies.

ng1

Technical User
Aug 22, 2007
39
US
Is there a way to put a subtotal on a separate line? I am trying to subtotal by $1. I want the word 'total' printed as well.

ex.

input file

111 a b
111 x d
111 j k
222 j 3
222 l 4
333 m 4
444 x e
444 3 o


desired output

111 a b
111 x d
111 j k
111 o b
total 4
222 j 3
222 l 4
total 2
333 m 4
total 1
444 x e
444 3 o
444 4 l
total 3


 
What have you tried?

You just need to detect when $1 is different to the previous $1, print the subtotal, and reset it to 0.

Then for every line add one to the subtotal and print the line.

You will need to print the final subtotal from the END { } clause.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top