-
-
Notifications
You must be signed in to change notification settings - Fork 105
Glasgow | 26-JUL-SDC | Shreef Ibrahim| Sprint 3 | Implement- shell- tools #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let flag = ""; | ||
| let lineNumber = 1; | ||
|
|
||
| for (const arg of args) { | ||
|
|
||
| // Check for flags | ||
| if (arg === "-n" || arg === "-b") { | ||
| flag = arg; | ||
| continue; | ||
| } | ||
|
|
||
| // Read the file | ||
| const content = fs.readFileSync(arg, "utf8"); | ||
|
|
||
| // split into lines | ||
| const lines = content.split("\n"); | ||
|
|
||
| // Remove the extra empty line if the file ends with a new line | ||
| if (lines[lines.length - 1] === "") { | ||
| lines.pop(); | ||
| } | ||
|
|
||
|
|
||
| for (const line of lines) { | ||
|
|
||
| if (flag === "-n") { | ||
| console.log(`${lineNumber}\t${line}`); | ||
| lineNumber++; | ||
| } | ||
|
|
||
| else if (flag === "-b") { | ||
|
|
||
| if (line === "") { | ||
| console.log(""); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What character does real cat place between the line number and the text — is it spaces, or something else? Try copying a line of real cat -b output and your own into the same file; do they line up? |
||
| } else { | ||
| console.log(`${String(lineNumber).padStart(6)} ${line}`); | ||
| lineNumber++; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| else { | ||
| console.log(line); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
| const args = process.argv.slice(2); | ||
|
|
||
| let onePerLine = false; | ||
| let showAll = false; | ||
| let target = "."; | ||
|
|
||
| for (const arg of args) { | ||
|
|
||
| if (arg === "-1") { | ||
| onePerLine = true; | ||
| } | ||
| else if (arg === "-a") { | ||
| showAll = true; | ||
| } | ||
| else { | ||
| target = arg; | ||
| } | ||
| } | ||
|
|
||
| const stats = fs.statSync(target); | ||
| if (stats.isFile()) { | ||
| console.log(path.basename(target)); | ||
| } else { | ||
|
|
||
| let files = fs.readdirSync(target); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Run ls -1 -a sample-files for real and compare it to yours, line by line. Which two entries does the real one list that yours doesn't? Does fs.readdirSync ever return those, and if not, where could they come from? |
||
|
|
||
| if (!showAll) { | ||
| files = files.filter(file => !file.startsWith(".")); | ||
| } | ||
| if (onePerLine) { | ||
|
|
||
| for (const file of files) { | ||
| console.log(file); | ||
| } | ||
|
|
||
| } else { | ||
| console.log(files.join(" ")); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let showWords = false; | ||
| let showChars = false; | ||
| let showLines = false; | ||
| let fileNames = []; | ||
|
|
||
| // Check flags and file names | ||
| for (let item of args) { | ||
| if (item === "-l") { | ||
| showLines = true; | ||
| } else if (item === "-w") { | ||
| showWords = true; | ||
| } else if (item === "-c") { | ||
| showChars = true; | ||
| } else { | ||
| fileNames.push(item); | ||
| } | ||
| } | ||
|
|
||
| // If no flags are given, show everything | ||
| if (!showLines && !showWords && !showChars) { | ||
| showLines = true; | ||
| showWords = true; | ||
| showChars = true; | ||
| } | ||
|
|
||
| for (let fileName of fileNames) { | ||
| let text = fs.readFileSync(fileName, "utf8"); | ||
|
|
||
| let totalLines = text.split("\n").length - 1; | ||
| let totalWords = text.trim().split(/\s+/).length; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would totalWords come out as for a completely empty file, given "".split(/\s+/)? What does real wc report for one? It's not in the test set — but is the count you'd produce the one you'd want? |
||
| let totalChars = Buffer.byteLength(text); | ||
|
|
||
| let output = ""; | ||
|
|
||
| if (showLines) output += totalLines + " "; | ||
| if (showWords) output += totalWords + " "; | ||
| if (showChars) output += totalChars + " "; | ||
|
|
||
| output += fileName; | ||
|
|
||
| console.log(output); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try running your wc sample-files/* next to the real wc sample-files/*. How many lines does each print? What does the real tool add at the bottom when you hand it more than one file - and where would that value need to be built up in your loop? |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put your cat -n sample-files/1.txt beside the real one and look at the space before the 1. What's different about how the number sits in the column? You already solved this exact thing somewhere else in this same file — where?