Skip to content

Commit 007ee54

Browse files
SaraTahir28cyf
authored andcommitted
Adding implementation to count number of lines
1 parent fdbd244 commit 007ee54

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

implement-shell-tools/cat/mycat.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { program } from "commander";
2+
import {promises as fs} from "node:fs"
3+
4+
5+
6+
//configuring the program here to run flags.
7+
8+
program
9+
.name("cat")
10+
.description("Concatenate and print files")
11+
.option("-n, --number", "Number all output lines")
12+
.option("-b, --number-nonblank", "Number non-blank output lines")
13+
.argument("<files...>", "File paths to display")
14+
.parse(process.argv);//Parse command line arguments (reads process.argv and interprets it)
15+
16+
const options = program.opts();
17+
const files = program.args; // Array of file paths passed as arguments
18+
19+
let lineNumber = 1; // shared across all files, like GNU cat -n)
20+
21+
for (const file of files) {
22+
const content = await fs.readFile(file, "utf-8");
23+
const lines = content.split("\n");
24+
25+
for (const line of lines) {
26+
27+
if (options.number) {
28+
console.log(`${lineNumber.toString().padStart(6)} ${line}`); // adding padding for formatting
29+
lineNumber++;
30+
}
31+
else{
32+
console.log(line);
33+
}
34+
}
35+
}
36+
37+
38+

0 commit comments

Comments
 (0)