Created
October 7, 2022 16:43
-
-
Save peterbe/6dd75887c70dd5925dfb27924d365bb5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import sys | |
from collections import Counter | |
def main(paths, n=100): | |
counter = Counter() | |
for path in paths: | |
print(path) | |
with open(path) as f: | |
for line in f: | |
if line.strip(): | |
counter[line.rstrip()] += 1 | |
largest = max(counter.values()) if counter else 0 | |
ljust = len(str(largest)) | |
for line, count in counter.most_common(n): | |
print(str(count).ljust(ljust), line) | |
if __name__ == "__main__": | |
my_parser = argparse.ArgumentParser( | |
description="Count the most common lines in files" | |
) | |
my_parser.add_argument( | |
"-n", | |
metavar="cap", | |
type=int, | |
default=100, | |
help="Max number shown", | |
) | |
my_parser.add_argument( | |
"paths", | |
# metavar="path", | |
nargs="+", | |
type=str, | |
help="the path to list", | |
) | |
args = my_parser.parse_args() | |
sys.exit(main(**vars(args))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment