Compare commits
2 Commits
e6c18957e6
...
fbadca49ac
Author | SHA1 | Date |
---|---|---|
perro tuerto | fbadca49ac | |
perro tuerto | c27cc4b41f |
|
@ -1,4 +1,4 @@
|
|||
# Exercise 1
|
||||
# Exercise 4
|
||||
# 1. Getting help with -help or -h.
|
||||
# 2. A least three arguments that are flags, meaning they don’t take an extra
|
||||
# argument but simply putting them on the command line turns something on.
|
||||
|
@ -10,12 +10,12 @@
|
|||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filename')
|
||||
parser.add_argument('-o1', '--opt1')
|
||||
parser.add_argument('-o2', '--opt2')
|
||||
parser.add_argument('-o3', '--opt3')
|
||||
parser.add_argument('-f1', '--flag1', action='store_true')
|
||||
parser.add_argument('-f2', '--flag2', action='store_true')
|
||||
parser.add_argument('-f3', '--flag3', action='store_true')
|
||||
parser.add_argument("filename")
|
||||
parser.add_argument("-o1", "--opt1")
|
||||
parser.add_argument("-o2", "--opt2")
|
||||
parser.add_argument("-o3", "--opt3")
|
||||
parser.add_argument("-f1", "--flag1", action="store_true")
|
||||
parser.add_argument("-f2", "--flag2", action="store_true")
|
||||
parser.add_argument("-f3", "--flag3", action="store_true")
|
||||
args = parser.parse_args()
|
||||
print(args)
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
# Exercise 2
|
||||
# Exercise 5
|
||||
# 1. cat command
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('paths', nargs="+", type=Path)
|
||||
parser.add_argument('-b', '--number-nonblank', default=False, action="store_true")
|
||||
parser.add_argument('-n', '--number', default=False, action="store_true")
|
||||
parser.add_argument("paths", nargs="+", type=Path)
|
||||
parser.add_argument(
|
||||
"-b", "--number-nonblank", default=False, action="store_true"
|
||||
)
|
||||
parser.add_argument("-n", "--number", default=False, action="store_true")
|
||||
args = parser.parse_args()
|
||||
count = 1
|
||||
rest = 0
|
||||
|
@ -15,16 +17,16 @@ out = []
|
|||
|
||||
for path in args.paths:
|
||||
if path.is_file():
|
||||
for line in open(path, 'r').readlines():
|
||||
for line in open(path, "r").readlines():
|
||||
blank = len(line.strip()) == 0 and args.number_nonblank
|
||||
if blank:
|
||||
out.append({"ln": 0, "txt": ''})
|
||||
out.append({"ln": 0, "txt": ""})
|
||||
else:
|
||||
out.append({"ln": count, "txt": line.rstrip()})
|
||||
count += 1
|
||||
|
||||
for el in out:
|
||||
line = ''
|
||||
line = ""
|
||||
blank = len(el["txt"].strip()) == 0 and args.number_nonblank
|
||||
if args.number and not blank:
|
||||
total_width = len(str(out[-1]["ln"] - rest))
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Exercise 2
|
||||
# Exercise 6
|
||||
# 1. find command
|
||||
|
||||
import sys
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Exercise 2
|
||||
# Exercise 7
|
||||
# 1. grep command
|
||||
|
||||
import re
|
||||
|
@ -40,9 +40,9 @@ def grep(path, args):
|
|||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('paths', nargs="+", type=Path)
|
||||
parser.add_argument('-i', '--ignore-case', default=False, action="store_true")
|
||||
parser.add_argument('-E', '--extended-regexp', default=False, type=str)
|
||||
parser.add_argument("paths", nargs="+", type=Path)
|
||||
parser.add_argument("-i", "--ignore-case", default=False, action="store_true")
|
||||
parser.add_argument("-E", "--extended-regexp", default=False, type=str)
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
if sys.argv[1] != "-E" and sys.argv[1] != "-i":
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Exercise 2
|
||||
# Exercise 8
|
||||
# 1. cut command
|
||||
|
||||
import re
|
||||
|
@ -31,9 +31,9 @@ def parse(fields):
|
|||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('stream', default=sys.stdin, nargs="?")
|
||||
parser.add_argument('-d', '--delimiter', default=False, type=str)
|
||||
parser.add_argument('-f', '--fields', type=str, required=True)
|
||||
parser.add_argument("stream", default=sys.stdin, nargs="?")
|
||||
parser.add_argument("-d", "--delimiter", default=False, type=str)
|
||||
parser.add_argument("-f", "--fields", type=str, required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
for line in args.stream:
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
# Exercise 9
|
||||
# 1. sed command
|
||||
|
||||
import re
|
||||
import sys
|
||||
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:
|
||||
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(
|
||||
"-e", "--expression", required=True, type=str, action="append"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
res = ""
|
||||
|
||||
for exp in args.expression:
|
||||
exp = parse(exp)
|
||||
for stream in args.stream:
|
||||
if isinstance(args.stream, list):
|
||||
stream = Path(stream).read_text()
|
||||
else:
|
||||
stream = stream.strip()
|
||||
if res == "":
|
||||
res = stream
|
||||
res = re.sub(exp["rgx"], exp["rep"], res, count=exp["n"])
|
||||
print(res)
|
Loading…
Reference in New Issue