38 lines
900 B
Python
Executable File
38 lines
900 B
Python
Executable File
#!/usr/bin/env python
|
|
# Exercise 11
|
|
# uniq command
|
|
|
|
import sys
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("stream", default=sys.stdin, nargs="*")
|
|
parser.add_argument("-i", "--ignore-case", action="store_true")
|
|
parser.add_argument("-c", "--count", action="store_true")
|
|
args = parser.parse_args()
|
|
lines = []
|
|
counter = None
|
|
|
|
for stream in args.stream:
|
|
if isinstance(args.stream, list):
|
|
for line in Path(stream).open().readlines():
|
|
lines.append(line.rstrip())
|
|
else:
|
|
lines.append(stream.strip())
|
|
|
|
if args.ignore_case:
|
|
lines = list(map(lambda l: l.lower(), lines))
|
|
|
|
if args.count:
|
|
counter = {}
|
|
for line in lines:
|
|
counter.setdefault(line, 0)
|
|
counter[line] += 1
|
|
|
|
lines = sorted(set(lines))
|
|
|
|
for line in lines:
|
|
msg = line if counter is None else f"{counter[line]} {line}"
|
|
print(msg)
|