Compare commits

...

4 Commits

Author SHA1 Message Date
perro tuerto 1f84479a80 Ejercicio 11 2023-03-03 11:59:57 -08:00
perro tuerto 7e29a54164 Renombre 2023-03-03 11:03:18 -08:00
perro tuerto 2453486213 Cambio de permisos a ejecución 2023-03-03 10:59:00 -08:00
perro tuerto b6e0169546 Renombre 2023-03-03 10:49:43 -08:00
11 changed files with 64 additions and 30 deletions

2
exercises/ex04/ex04.py → exercises/ex04/help Normal file → Executable file
View File

@ -1,4 +1,6 @@
#!/usr/bin/env python
# Exercise 4
# help arg
# 1. Getting help with -help or -h.
# 2. A least three arguments that are flags, meaning they dont take an extra
# argument but simply putting them on the command line turns something on.

3
exercises/ex05/ex05.py → exercises/ex05/cat Normal file → Executable file
View File

@ -1,5 +1,6 @@
#!/usr/bin/env python
# Exercise 5
# 1. cat command
# cat command
import argparse
from pathlib import Path

3
exercises/ex06/ex06.py → exercises/ex06/find Normal file → Executable file
View File

@ -1,5 +1,6 @@
#!/usr/bin/env python
# Exercise 6
# 1. find command
# find command
import sys
import argparse

3
exercises/ex07/ex07.py → exercises/ex07/grep Normal file → Executable file
View File

@ -1,5 +1,6 @@
#!/usr/bin/env python
# Exercise 7
# 1. grep command
# grep command
import re
import sys

3
exercises/ex08/ex08.py → exercises/ex08/cut Normal file → Executable file
View File

@ -1,5 +1,6 @@
#!/usr/bin/env python
# Exercise 8
# 1. cut command
# cut command
import re
import sys

37
exercises/ex09/ex09.py → exercises/ex09/sed Normal file → Executable file
View File

@ -1,5 +1,6 @@
#!/usr/bin/env python
# Exercise 9
# 1. sed command
# sed command
import re
import sys
@ -7,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(

3
exercises/ex10/ex10.py → exercises/ex10/sort Normal file → Executable file
View File

@ -1,5 +1,6 @@
#!/usr/bin/env python
# Exercise 10
# 1. sort command
# sort command
import sys
import argparse

1
exercises/ex11/cut Symbolic link
View File

@ -0,0 +1 @@
../ex08/cut

1
exercises/ex11/sed Symbolic link
View File

@ -0,0 +1 @@
../ex09/sed

1
exercises/ex11/sort Symbolic link
View File

@ -0,0 +1 @@
../ex10/sort

37
exercises/ex11/uniq Executable file
View File

@ -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)