Skip to main content

Add a sumsizes script

ID
c79d80b
date
2023-06-25 07:47:40+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
f8cf567
message
Add a `sumsizes` script
changed files
2 files, 34 additions

Changed files

text/README.md (1468) → text/README.md (1768)

diff --git a/text/README.md b/text/README.md
index 7a82b53..9d3c602 100644
--- a/text/README.md
+++ b/text/README.md
@@ -34,6 +34,17 @@ These are utilities for manipulating streams of text; I consider them in a simil
   </dd>
 
   <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/text/sumsizes">
+      <code>sumsizes < [PATH]</code>
+    </a>
+  </dt>
+  <dd>
+    prints a human-readable data size for the numbers passed on stdin.
+    <pre><code>echo -e '100 \n 201287 \n 3190817' | sumsizes</code></pre>
+  </dd>
+
+
+  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/text/randline">
       <code>tally < [PATH]</code>
     </a>

text/sumsizes (0) → text/sumsizes (378)

diff --git a/text/sumsizes b/text/sumsizes
new file mode 100755
index 0000000..07637b3
--- /dev/null
+++ b/text/sumsizes
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+"""
+Given a list of numbers on stdin, add them together and print the result
+as a human-readable data size.
+
+Example:
+
+    echo -e '100 \n 201287 \n 3190817' | sumsizes
+
+"""
+
+import sys
+
+import humanize
+
+
+if __name__ == '__main__':
+    total = sum(
+        int(line)
+        for line in sys.stdin.readlines()
+    )
+
+    print(humanize.naturalsize(total))