Skip to content

Commit abb6c89

Browse files
committed
cat, ls and wc tasks compelted
1 parent 9f69078 commit abb6c89

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

.DS_Store

10 KB
Binary file not shown.

implement-shell-tools/.DS_Store

6 KB
Binary file not shown.

implement-shell-tools/cat/cat.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sys
2+
from pathlib import Path
3+
4+
def cleanInput(listOfFiles):
5+
cleanLinesArr = []
6+
7+
for file in listOfFiles:
8+
grabbedText = Path(file).read_text(encoding="utf-8")
9+
splitLines = grabbedText.split("\n")
10+
cleanLinesArr.extend(splitLines)
11+
12+
return cleanLinesArr
13+
14+
args = sys.argv[1:]
15+
flag = None
16+
restIsFiles = []
17+
18+
if len(args) > 0 and args[0] and args[0][0] == "-":
19+
flag = args[0]
20+
restIsFiles = args[1:]
21+
else:
22+
flag = None
23+
restIsFiles = args
24+
25+
def takeSpecifiedAction(cleanLinesArr, flag):
26+
countingOnlyFullLines = 1
27+
28+
for file in cleanLinesArr:
29+
if not flag:
30+
print(file)
31+
elif flag == "-n":
32+
print(f"{countingOnlyFullLines} {file}")
33+
countingOnlyFullLines += 1
34+
elif flag == "-b":
35+
if file == "":
36+
print("")
37+
else:
38+
print(f"{countingOnlyFullLines} {file}")
39+
countingOnlyFullLines += 1
40+
else:
41+
print("incorrect flag")
42+
43+
lines = cleanInput(restIsFiles)
44+
takeSpecifiedAction(lines, flag)

implement-shell-tools/ls/ls.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
import os
3+
4+
# `ls -1`
5+
def showAllFilesInDir(directory):
6+
listOfFiles = os.listdir(directory)
7+
8+
for eachFile in listOfFiles:
9+
if eachFile[0] != ".":
10+
print(eachFile)
11+
12+
# `ls -1 sample-files`
13+
def showVisibleInSampleFiles():
14+
listOfFiles = os.listdir("sample-files")
15+
16+
for eachFile in listOfFiles:
17+
if eachFile[0] != ".":
18+
print(eachFile)
19+
20+
# `ls -1 -a sample-files`
21+
def showAllInSampleFiles():
22+
listOfFiles = os.listdir("sample-files")
23+
24+
for eachFile in listOfFiles:
25+
print(eachFile)
26+
27+
argv = sys.argv[1:]
28+
29+
if "-a" in argv:
30+
showAllInSampleFiles()
31+
elif "sample-files" in argv:
32+
showVisibleInSampleFiles()
33+
else:
34+
showAllFilesInDir(".")

implement-shell-tools/wc/wc.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import sys
2+
from pathlib import Path
3+
4+
def calculateCounts(inputFiles):
5+
return {
6+
"lines": len(inputFiles.split("\n")) - 1,
7+
"words": len(inputFiles.split()),
8+
"bytes": len(inputFiles),
9+
}
10+
11+
# * `wc -l sample-files/3.txt`
12+
# * `wc -l sample-files/*`
13+
def countLines(listOfFiles):
14+
for file in listOfFiles:
15+
content = Path(file).read_text(encoding="utf-8")
16+
17+
counts = calculateCounts(content)
18+
print(f"{counts['lines']} {file}")
19+
20+
# * `wc -w sample-files/3.txt`
21+
def countWords(listOfFiles):
22+
for file in listOfFiles:
23+
content = Path(file).read_text(encoding="utf-8")
24+
25+
# const wordsCounted = content.split(" ").filter(word => word !== "").length;
26+
counts = calculateCounts(content)
27+
print(f"{counts['words']} {file}")
28+
29+
# * `wc -c sample-files/3.txt`
30+
def countBytes(listOfFiles):
31+
for file in listOfFiles:
32+
content = Path(file).read_text(encoding="utf-8")
33+
counts = calculateCounts(content)
34+
print(f"{counts['bytes']} {file}")
35+
36+
# * `wc sample-files/*`
37+
def countAll(listOfFiles):
38+
for file in listOfFiles:
39+
content = Path(file).read_text(encoding="utf-8")
40+
counts = calculateCounts(content)
41+
print(f"{counts['lines']} {counts['words']} {counts['bytes']} {file}")
42+
43+
argv = sys.argv[1:]
44+
files = [arg for arg in argv if not arg.startswith("-")]
45+
46+
if "-l" in argv:
47+
countLines(files)
48+
elif "-w" in argv:
49+
countWords(files)
50+
elif "-c" in argv:
51+
countBytes(files)
52+
else:
53+
countAll(files)

0 commit comments

Comments
 (0)