Skip to content

Commit 6f7c430

Browse files
committed
Implement cat tool with -n and -b flags
1 parent 3f26722 commit 6f7c430

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

implement-shell-tools/cat/cat*.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const fs = require("fs");
2+
const folderPath = "./sample-files";
3+
const content = fs.readdirSync(folderPath);
4+
5+
const txtFiles = content.filter((file) => file.endsWith(".txt"));
6+
process.stdout.write(txtFiles.join("\n"));

implement-shell-tools/cat/cat-b.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const fs = require("fs");
2+
const filePath = "./sample-files/3.txt";
3+
const content = fs.readFileSync(filePath, "utf-8");
4+
5+
let count = 1;
6+
const numberLines = content.split("\n").map((line) => {
7+
if (line.trim() !== "") {
8+
return `${count++} ${line.trim()}`;
9+
}
10+
return "";
11+
});
12+
process.stdout.write(numberLines.join("\n"));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require("fs");
2+
const folderPath = "./sample-files";
3+
const content = fs.readdirSync(folderPath);
4+
5+
const txtFiles = content.filter((file) => file.endsWith(".txt"));
6+
const numberedLine = txtFiles.map((line, index) => {
7+
return `${index + 1} ${line}`;
8+
});
9+
process.stdout.write(numberedLine.join("\n"));

implement-shell-tools/cat/cat-n.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require("fs");
2+
const filePath = "./sample-files/1.txt";
3+
const content = fs.readFileSync(filePath, "utf-8");
4+
5+
const lineNumber = content.split("\n").map((line, index) => {
6+
return `${index + 1} ${line}`;
7+
});
8+
process.stdout.write(lineNumber.join("\n"));

implement-shell-tools/cat/cat.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const fs = require("fs");
2+
const filePath = "./sample-files/1.txt";
3+
4+
const content = fs.readFileSync(filePath, "utf-8");
5+
6+
process.stdout.write(content);

0 commit comments

Comments
 (0)