Sum Total Number Of Rows Affected By Queries

Stupid command line tricks: I often run SQL jobs that can take many days to fully execute, and I always pipe the error and stdout to a file. As such, you end up with lots of lines like this in the output:Query OK, 81218 rows affected (52.76 sec)Here’s a silly one-liner that will quickly total up ALLLLLL the rows that have been affected. (this assumes the output is being directed to a file named do_archive.out)

grep affected do_archive.out | cut -d' ' -f3 | perl -nle '$sum += $_ } END { print $sum'

Want to format your number with commas?  Pipe it through this:

sed ':a;s/\B[0-9]\{3\}\>/,&/;ta'

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: