Grand totals in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grand totals in awk
# 1  
Old 05-21-2014
Grand totals in awk

I have a one-liner script like this that gives a total of everything in various directories:

Code:
for i in *;
do (cd $i && cd statelist && echo $i && ls -la |awk 'NR>3 {SUM += $5}\
 END {  print "Total number of elements " SUM }');done

It works just great but at the end I want to print a grand total of all the numbers in column 5. I tried to tack on another SUM statement but it didn't work. Not sure how this code should work for grand total.

Last edited by vbe; 05-21-2014 at 01:10 PM..
# 2  
Old 05-21-2014
It doesn't work because you are running awk 937 times to count 937 directories, counting 937 separate "grand" totals.

Also: If your 'one liner' is three terminals wide, it probably shouldn't be a one-liner.

Also you can simplify it a lot. The 'echo' is pointless (and may actually be a bug). You can glob for 'statelist' instead of searching for it in every possible file and folder too. And you don't even need to cd -- or loop at all, for that matter -- to ls -la it.

Code:
ls -la */statelist | awk '!/^(total|d)/ && (NR>5) {SUM += $5} END {  print "Total number of elements " SUM }'

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-21-2014
you use find -type d to insure that ${i} will only find directories
This User Gave Thanks to blackrageous For This Post:
# 4  
Old 05-21-2014
That's a good idea, find will be simpler than ls here, but we want only files, not dirs...

Code:
find */statelist -type f -printf "%s\n" | awk '{ SUM+=$1 } END { print "sum " SUM }'

Where %s, to find, means 'size in bytes'. So we don't need to do any filtering in awk at all.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to wait for a grand child to finish?

Hello all, I have a very basic question. I have a requirement where in, I have a main process which forks a child process, which execs and runs a c executable corresponding to a daemon. In the c executable corresponding to a daemon, as everyone does, I fork another child process, and as part of... (7 Replies)
Discussion started by: sai2krishna
7 Replies

2. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

3. UNIX for Dummies Questions & Answers

Fritz Chess, Grand Master Challenge, running on Ubuntu

Chess game. Has anyone installed and run Fritz Grand Master Challenge chess on Ubuntu 11.10/ Unix? I have read about disappointments with different versions of wine, but not successes. (0 Replies)
Discussion started by: Maaattt
0 Replies

4. Shell Programming and Scripting

Totals in a file - incorrectly displaying

Afternoon, I have a script which creates/modifies data into a formatted csv. The trailer record should display 2 columns, the first is a static entry of "T" to identify it as a trailer record. The 2nd is a total of amounts in a column throughout the entire file. My total isn't displaying... (8 Replies)
Discussion started by: mcclunyboy
8 Replies

5. Shell Programming and Scripting

Report Totals

Hello, I have written a script in a previous server and its being migrated to a new server. I'm trying to debug my script since i've had to make minor changes to it to get it to work. I'm having a hard time getting my totals to populate here is the syntax DUMP_COUNT=`sqlplus -S... (4 Replies)
Discussion started by: senormarquez
4 Replies

6. Shell Programming and Scripting

summarising totals in awk

awk ' FILENAME == "all" { balance += substr($0,17,13) dt = substr($0,6,8) } END { for ( name in balance ) printf("%013s %3s of %8s\n", balance/100,name,dt) | "sort " } ' all > summation using this code i wanted to take summary totals of... (3 Replies)
Discussion started by: paresh n doshi
3 Replies

7. Shell Programming and Scripting

Calculating totals in AWK

Hello, With the following small script I list the size of documents belonging to a certain user by each time selecting the bytes-field of that file ($7). Now it fills the array with every file it finds so in the end the output of some users contains up to 200.000 numbers. So how can I calculate... (7 Replies)
Discussion started by: Hille
7 Replies
Login or Register to Ask a Question