Ejercicio 11
This commit is contained in:
parent
7e29a54164
commit
1f84479a80
|
@ -8,34 +8,20 @@ import argparse
|
|||
from pathlib import Path
|
||||
|
||||
|
||||
def check(expression, exp):
|
||||
error = False
|
||||
if (
|
||||
expression[-1] not in "/g"
|
||||
or exp[0] not in "s"
|
||||
or len(exp) not in range(3, 5)
|
||||
or (expression[-1] in "g" and len(exp) != 4)
|
||||
or (expression[-1] in "/" and len(exp) == 4)
|
||||
):
|
||||
error = True
|
||||
if error:
|
||||
def parse(expression):
|
||||
try:
|
||||
match = re.fullmatch(r's/(.*)/(.*)/(g{0,1})', expression)
|
||||
count = 0 if match.group(3) == "g" else 1
|
||||
return {
|
||||
"rgx": re.compile(match.group(1)),
|
||||
"rep": match.group(2),
|
||||
"n": count
|
||||
}
|
||||
except Exception:
|
||||
print(f"sed: expresión «{expression}» inválida")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def parse(expression):
|
||||
count = 0
|
||||
exp = list(filter(None, expression.split("/")))
|
||||
check(expression, exp)
|
||||
if len(exp) == 3:
|
||||
count = 1
|
||||
return {
|
||||
"rgx": re.compile(exp[1]),
|
||||
"rep": exp[2],
|
||||
"n": count,
|
||||
}
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("stream", default=sys.stdin, nargs="*")
|
||||
parser.add_argument(
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
../ex08/cut
|
|
@ -0,0 +1 @@
|
|||
../ex09/sed
|
|
@ -0,0 +1 @@
|
|||
../ex10/sort
|
|
@ -0,0 +1,37 @@
|
|||
#!/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)
|
Loading…
Reference in New Issue