From 95e2b086f37c2ed4f0f2dfa154521a1782eba5aa Mon Sep 17 00:00:00 2001 From: Tristan Smith Date: Fri, 5 Apr 2024 22:49:10 -0400 Subject: [PATCH] Add sizegetter.py to calculate total size of files containing a specific keyword --- sizegetter.py | 29 +++++++++++++++++++++++++++++ zipremove.py | 0 zipsize | 17 +++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 sizegetter.py create mode 100644 zipremove.py create mode 100644 zipsize diff --git a/sizegetter.py b/sizegetter.py new file mode 100644 index 0000000..356bdaa --- /dev/null +++ b/sizegetter.py @@ -0,0 +1,29 @@ +import re +import sys + +def calculate_total_size(filename, keyword): + pattern = re.compile(rf'{re.escape(keyword)}.*?(\d+(?:\.\d+)?)\s(MiB|GiB)') + total_size_mib = 0 + + with open(filename, 'r', encoding='utf-8') as file: + for line in file: + match = pattern.search(line) + if match: + size, unit = match.groups() + size = float(size) + if unit == "GiB": + size *= 1024 # Convert GiB to MiB + total_size_mib += size + + total_size_gib = total_size_mib / 1024 + return total_size_mib, total_size_gib + +if __name__ == "__main__": + if len(sys.argv) < 3: + print("Usage: python script.py ") + sys.exit(1) + + filename = sys.argv[1] + keyword = sys.argv[2] + total_size_mib, total_size_gib = calculate_total_size(filename, keyword) + print(f"Total size of files containing '{keyword}': {total_size_gib:.2f} GiB ({total_size_mib:.0f} MiB)") diff --git a/zipremove.py b/zipremove.py new file mode 100644 index 0000000..e69de29 diff --git a/zipsize b/zipsize new file mode 100644 index 0000000..98ac49e --- /dev/null +++ b/zipsize @@ -0,0 +1,17 @@ +#!/bin/bash + +awk 'BEGIN{CONVFMT="%.2f"} +{ + if ($1 ~ /^[0-9]+$/) { + byte=$1; + if (byte >= 1073741824) + {printf("%.2f GB\t%s\n", byte/1073741824, $4)} + else if (byte >= 1048576) + {printf("%.2f MB\t%s\n", byte/1048576, $4)} + else if (byte >= 1024) + {printf("%.2f KB\t%s\n", byte/1024, $4)} + else + {print $1 " B\t" $4} + } + else {print $0} +}'