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