diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 00000000..4b2e57b5 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,42 @@ +import argparse +import os + +# set argument parser +parser = argparse.ArgumentParser(description="Python implementation of cat command") + +# to number all lines +parser.add_argument("-n", action="store_true", help="Number all lines") + +# to number non-blank lines +parser.add_argument("-b", action="store_true", help="Number non-blank lines (overrides -n)") + +# To raed files +parser.add_argument("files", nargs="+", help="Files to read") + +args = parser.parse_args() + +if args.n and args.b: + args.n = False + +line_number = 1 + +for file in args.files: + if not os.path.isfile(file): + print(f"cat: {file}: No such file or directory") + continue + + + with open(file, "r") as f: + for line in f: + if args.b: + if line.strip(): #to number non blank lines only + print(f"{line_number:6}\t{line}", end="") + line_number += 1 + else: + print(line, end="") # to print blank line with number + + elif args.n: + print(f"{line_number:6}\t{line}", end="") # to print number all lines + line_number += 1 + else: + print(line, end ="") # to print content of files diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 00000000..2fd72018 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,16 @@ +import argparse +import os + +parser = argparse.ArgumentParser(description="dispaly one file per line from ls directory") + +parser.add_argument("-1", action="store_true", help="List one file per line") +parser.add_argument("-a", action="store_true", help="Include hidden files (those starting with .)") +parser.add_argument("path", nargs="?", default=".", help="The directory to list (default: current directory)") + +args = parser.parse_args() + +for filename in sorted(os.listdir(args.path)): + if not args.a and filename.startswith("."): + # skip hidden files unless -a is provided + continue + print(filename) \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 00000000..24630005 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,62 @@ +import argparse +import os + +parser = argparse.ArgumentParser(description="Python implementation of wc command") + +parser.add_argument("-l", action="store_true", help="Print line count") + +parser.add_argument("-w", action="store_true", help="Print word count") + +parser.add_argument("-c", action="store_true", help="Print byte count") + +parser.add_argument("files", nargs="+", help="Files to process") + +args = parser.parse_args() + +total_lines = 0 +total_words = 0 +total_bytes = 0 + +multiple_files = len(args.files) > 1 # to store totals if muliple files + +# Helper function to print counts in wc style +def print_counts(lines, words, bytes_, filename): + output = [] + if args.l: + output.append(str(lines)) + if args.w: + output.append(str(words)) + if args.c: + output.append(str(bytes_)) + + # If no flags are given, print all three counts + if not output: + output = [f"{lines:>7}", f"{words:>7}", f"{bytes_:>7}"] + + print(" ".join(output), filename) + +for file in args.files: + if not os.path.isfile(file): + print(f"wc: {file}: No such file or directory") + continue + + with open(file, "r", encoding="utf-8") as f: + content = f.read() + lines = content.count("\n") + words = len(content.split()) + tbytes = os.path.getsize(file) + + + total_lines += lines + total_words += words + total_bytes += tbytes + + # Print counts per file + print_counts(lines, words, tbytes, file) + +#to print total output +if multiple_files: + print_counts(total_lines, total_words, total_bytes, "total") + + + \ No newline at end of file