Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created October 7, 2022 16:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbe/6dd75887c70dd5925dfb27924d365bb5 to your computer and use it in GitHub Desktop.
Save peterbe/6dd75887c70dd5925dfb27924d365bb5 to your computer and use it in GitHub Desktop.
#!/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