Skip to content

Commit 5b007a2

Browse files
committed
complete jq exersice
1 parent 4350f48 commit 5b007a2

File tree

13 files changed

+111
-0
lines changed

13 files changed

+111
-0
lines changed

implement-shell-tools/cat/cat.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
// arguments
5+
const args = process.argv.slice(2);
6+
7+
let isNumbered = false;
8+
let isNumberNonEmpty = false;
9+
const filePaths = [];
10+
11+
// parse args
12+
args.forEach((arg) => {
13+
if (arg === "-n") {
14+
isNumbered = true;
15+
} else if (arg === "-b") {
16+
isNumberNonEmpty = true;
17+
} else {
18+
filePaths.push(arg);
19+
}
20+
});
21+
22+
// -b overrides -n
23+
if (isNumberNonEmpty) {
24+
isNumbered = false;
25+
}
26+
27+
let lineNumber = 1;
28+
29+
// process files
30+
filePaths.forEach((filePath, fileIndex) => {
31+
const content = fs.readFileSync(filePath, "utf-8");
32+
33+
// split lines but keep empty ones
34+
const lines = content.split("\n");
35+
36+
lines.forEach((line, index) => {
37+
const isLastLineOfLastFile =
38+
fileIndex === filePaths.length - 1 && index === lines.length - 1;
39+
40+
if (isNumberNonEmpty) {
41+
if (line !== "") {
42+
console.log(`${lineNumber} ${line}`);
43+
lineNumber++;
44+
} else {
45+
console.log(line);
46+
}
47+
} else if (isNumbered) {
48+
console.log(`${lineNumber} ${line}`);
49+
lineNumber++;
50+
} else {
51+
console.log(line);
52+
}
53+
});
54+
});

implement-shell-tools/ls/ls.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fs = require("fs");
2+
3+
// Get command line arguments
4+
const args = process.argv.slice(2);
5+
6+
// Default settings
7+
let showAll = false;
8+
let onePerLine = false;
9+
let dir = ".";
10+
11+
// Parse arguments
12+
args.forEach((arg) => {
13+
if (arg === "-a") {
14+
showAll = true;
15+
} else if (arg === "-1") {
16+
onePerLine = true;
17+
} else {
18+
dir = arg;
19+
}
20+
});
21+
22+
// Read directory contents
23+
let files = fs.readdirSync(dir);
24+
25+
// Filter hidden files if -a is NOT used
26+
if (!showAll) {
27+
files = files.filter((file) => !file.startsWith("."));
28+
}
29+
30+
// Output
31+
if (onePerLine) {
32+
files.forEach((file) => console.log(file));
33+
} else {
34+
console.log(files.join(" "));
35+
}

jq/script-01.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ set -euo pipefail
55
# The input for this script is the person.json file.
66
# TODO: Write a command to output the name of the person.
77
# Your output should be exactly the string "Selma", but should not contain any quote characters.
8+
9+
jq -r '.name' person.json

jq/script-02.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ set -euo pipefail
55
# The input for this script is the person.json file.
66
# TODO: Write a command to output the address of the person, all on one line, with a comma between each line.
77
# Your output should be exactly the string "35 Fashion Street, London, E1 6PX", but should not contain any quote characters.
8+
9+
jq -r '.address | join(", ")' person.json

jq/script-03.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ set -euo pipefail
55
# The input for this script is the person.json file.
66
# TODO: Write a command to output the name of the person, then a comma, then their profession.
77
# Your output should be exactly the string "Selma, Software Engineer", but should not contain any quote characters.
8+
9+
jq -r '"\(.name), \(.profession)"' person.json

jq/script-04.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ set -euo pipefail
66
# TODO: Write a command to output just the names of each player, one per line.
77
# Your output should contain 6 lines, each with just one word on it.
88
# Your output should not contain any quote characters.
9+
10+
jq -r '.[].name' scores.json

jq/script-05.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ set -euo pipefail
55
# The input for this script is the scores.json file.
66
# TODO: Write a command to output the names of each player, as well as their city.
77
# Your output should contain 6 lines, each with two words on it.
8+
9+
jq -r '.[] | "\(.name) \(.city)"' scores.json

jq/script-06.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ set -euo pipefail
66
# TODO: Write a command to output just the names of each player along with the score from their first attempt.
77
# Your output should contain 6 lines, each with one word and one number on it.
88
# The first line should be "Ahmed 1" with no quotes.
9+
10+
jq -r '.[] | "\(.name) \(.scores[0])"' scores.json

jq/script-07.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ set -euo pipefail
66
# TODO: Write a command to output just the names of each player along with the score from their last attempt.
77
# Your output should contain 6 lines, each with one word and one number on it.
88
# The first line should be "Ahmed 4" with no quotes.
9+
10+
jq -r '.[] | "\(.name) \(.scores[-1])"' scores.json

jq/script-08.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ set -euo pipefail
66
# TODO: Write a command to output just the names of each player along with the number of times they've played the game.
77
# Your output should contain 6 lines, each with one word and one number on it.
88
# The first line should be "Ahmed 3" with no quotes.
9+
10+
jq -r '.[] | "\(.name) \(.scores | length)"' scores.json

0 commit comments

Comments
 (0)