From 132d7cd0b6d7bd32fa416659324d44912337f73e Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Wed, 6 Aug 2025 21:13:57 +0100 Subject: [PATCH 1/8] python code for ls folder --- implement-shell-tools/ls/ls.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 implement-shell-tools/ls/ls.py 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 From c4f47a0a24f61f4154728ccd9e4dd0db36ba5a9c Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Wed, 6 Aug 2025 21:24:02 +0100 Subject: [PATCH 2/8] code for cat folder to print content of 1 and all files --- implement-shell-tools/cat/cat.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 implement-shell-tools/cat/cat.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 00000000..6f91b0cc --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,20 @@ +import argparse +import os + +parser = argparse.ArgumentParser(description="Python implementation of cat command") + +parser.add_argument("files", nargs="+", help="Files to read") + +args = parser.parse_args() + +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: + print(line, end ="") From aad36ba8648b9daadd710464c880ade1598018ad Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Wed, 6 Aug 2025 21:34:02 +0100 Subject: [PATCH 3/8] cat folder code upadate for -n flag --- implement-shell-tools/cat/cat.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 6f91b0cc..b1c87506 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -1,8 +1,12 @@ 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 raed files parser.add_argument("files", nargs="+", help="Files to read") args = parser.parse_args() @@ -17,4 +21,8 @@ with open(file, "r") as f: for line in f: - print(line, end ="") + if 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 From c7e390957c8a375d424c763060ccfd3885fb795c Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Wed, 6 Aug 2025 22:24:40 +0100 Subject: [PATCH 4/8] cat folder update code to implement -b flag --- implement-shell-tools/cat/cat.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index b1c87506..4b2e57b5 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -6,11 +6,18 @@ # 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: @@ -21,7 +28,14 @@ with open(file, "r") as f: for line in f: - if args.n: + 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: From 0c59ef134f94503cdfa1655c05591c691e93c3d1 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Thu, 7 Aug 2025 12:23:39 +0100 Subject: [PATCH 5/8] wc folder code for count data from muliple files --- implement-shell-tools/wc/wc.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 00000000..d257775a --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,39 @@ +import argparse +import os + +parser = argparse.ArgumentParser(description="Python implementation of wc command") + +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 + +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 = len(content.encode("utf-8")) + + + total_lines += lines + total_words += words + total_bytes += tbytes + + print(f"{lines:>7} {words:>7} {tbytes:>7} {file}") # to print data from per life + +#to print total output +if multiple_files: + print(f"{total_lines:>7} {total_words:>7} {total_bytes:>7} total") + + + \ No newline at end of file From 518eafb1b5cdcb1ae65fd11bc411847d8ea875e3 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Thu, 7 Aug 2025 13:01:13 +0100 Subject: [PATCH 6/8] wc folder code update to support -l flag --- implement-shell-tools/wc/wc.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index d257775a..e86f1cf5 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -3,6 +3,8 @@ parser = argparse.ArgumentParser(description="Python implementation of wc command") +parser.add_argument("-l", action="store_true", help="Print line count") + parser.add_argument("files", nargs="+", help="Files to process") args = parser.parse_args() @@ -28,12 +30,19 @@ total_lines += lines total_words += words total_bytes += tbytes - - print(f"{lines:>7} {words:>7} {tbytes:>7} {file}") # to print data from per life + + if args.l: + print(f"{lines:} {file}") + + else: + print(f"{lines:>3} {words:>3} {tbytes:>3} {file}") # to print data from per life #to print total output if multiple_files: - print(f"{total_lines:>7} {total_words:>7} {total_bytes:>7} total") + if args.l: + print(f"{total_lines:} total") + else: + print(f"{total_lines:>3} {total_words:>3} {total_bytes:>3} total") \ No newline at end of file From 40e512596c73ab810a5a0be75d1f34dbf92ed15d Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Thu, 7 Aug 2025 13:05:44 +0100 Subject: [PATCH 7/8] wc folder code update for -w flag support --- implement-shell-tools/wc/wc.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index e86f1cf5..64245f5f 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -5,6 +5,8 @@ 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("files", nargs="+", help="Files to process") args = parser.parse_args() @@ -34,6 +36,9 @@ if args.l: print(f"{lines:} {file}") + elif args.w: + print(f"{words:} {file}") + else: print(f"{lines:>3} {words:>3} {tbytes:>3} {file}") # to print data from per life @@ -41,6 +46,10 @@ if multiple_files: if args.l: print(f"{total_lines:} total") + + elif args.w: + print(f"{total_words:} total") + else: print(f"{total_lines:>3} {total_words:>3} {total_bytes:>3} total") From ac8c2decdd94e1f6b08c1dfd7b11aac4174eedaa Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Thu, 7 Aug 2025 14:19:28 +0100 Subject: [PATCH 8/8] wc folder code update to support -c flag --- implement-shell-tools/wc/wc.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index 64245f5f..4dbdcdf4 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -7,6 +7,8 @@ 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() @@ -26,8 +28,9 @@ content = f.read() lines = content.count("\n") words = len(content.split()) - tbytes = len(content.encode("utf-8")) + tbytes = os.path.getsize(file) + total_lines += lines total_words += words @@ -37,10 +40,13 @@ print(f"{lines:} {file}") elif args.w: - print(f"{words:} {file}") + print(f"{words:} {file}") + + elif args.c: + print(f"{tbytes:} {file}") else: - print(f"{lines:>3} {words:>3} {tbytes:>3} {file}") # to print data from per life + print(f"{lines:>3} {words:>3} {tbytes:>3} {file}") # to print data from per file #to print total output if multiple_files: