From 83e85d9e8c7f039a88f9585868fae1cd4f260e07 Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Mon, 16 Nov 2026 12:07:26 +0000 Subject: [PATCH 01/11] ls command done --- individual-shell-tools/ls/script-01.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/individual-shell-tools/ls/script-01.sh b/individual-shell-tools/ls/script-01.sh index 241b62f5e..0897042fa 100755 --- a/individual-shell-tools/ls/script-01.sh +++ b/individual-shell-tools/ls/script-01.sh @@ -13,3 +13,6 @@ fi # TODO: Write a command to list the files and folders in this directory. # The output should be a list of names including child-directory, script-01.sh, script-02.sh, and more. +# I use ls command to list the files and folders in this directory. +ls + From 695c68bdd9d67e9b1ea3683a30276f84bbd69c84 Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Mon, 16 Nov 2026 12:21:53 +0000 Subject: [PATCH 02/11] ls argument l, listing all the files in the directory --- individual-shell-tools/ls/script-02.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/individual-shell-tools/ls/script-02.sh b/individual-shell-tools/ls/script-02.sh index d0a5a10f4..719c6eafe 100755 --- a/individual-shell-tools/ls/script-02.sh +++ b/individual-shell-tools/ls/script-02.sh @@ -4,3 +4,5 @@ set -euo pipefail # TODO: Write a command which lists all of the files in the directory named child-directory. # The output should be a list of names: helper-1.txt, helper-2.txt, helper-3.txt. +# The command that lists all of the files in the directory named child-directory is ls child-directory +ls child-directory From 31130348db92161fa790b1730d71dadfe189d3b4 Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Mon, 16 Nov 2026 12:22:46 +0000 Subject: [PATCH 03/11] Used ls -R to list all the files and folders including the files inside them. --- individual-shell-tools/ls/script-03.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/individual-shell-tools/ls/script-03.sh b/individual-shell-tools/ls/script-03.sh index 781216d21..f519a299d 100755 --- a/individual-shell-tools/ls/script-03.sh +++ b/individual-shell-tools/ls/script-03.sh @@ -5,3 +5,6 @@ set -euo pipefail # TODO: Write a command which _recursively_ lists all of the files and folders in this directory _and_ all of the files inside those folders. # The output should be a list of names including: child-directory, script-01.sh, helper-1.txt (and more). # The formatting of the output doesn't matter. +# To list all the files and folders in this directory and all of the files inside those folders I use ls -R + +ls -R \ No newline at end of file From 9d1564502cd7b46ab03d614512fbec09a5565080 Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Mon, 16 Nov 2026 12:47:32 +0000 Subject: [PATCH 04/11] Used ls -tr1 or ls -t1 so sort the files --- individual-shell-tools/ls/script-04.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/individual-shell-tools/ls/script-04.sh b/individual-shell-tools/ls/script-04.sh index 72f3817b3..019c01ac3 100755 --- a/individual-shell-tools/ls/script-04.sh +++ b/individual-shell-tools/ls/script-04.sh @@ -15,9 +15,13 @@ echo "First exercise (sorted newest to oldest):" # TODO: Write a command which lists the files in the child-directory directory, one per line, sorted so that the most recently modified file is first. # The output should be a list of names in this order, one per line: helper-3.txt, helper-1.txt, helper-2.txt. - +# The command which lists the files in the child-directory directory, one per line, sorted so that the most recently modified file is first is ls -t child-directory +ls -t -1 child-directory echo "Second exercise (sorted oldest to newest):" # TODO: Write a command which does the same as above, but sorted in the opposite order (oldest first). # The output should be a list of names in this order, one per line: helper-2.txt, helper-1.txt, helper-3.txt. +# The command which sorts in the opposite order (oldest first) is ls -tr -1 child-directory. + +ls -tr -1 child-directory From 9fcd82d5ac7b1bcab70f038a4e976f860c34e66d Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Mon, 16 Nov 2026 13:01:59 +0000 Subject: [PATCH 05/11] Explain ls flags in comments and fix trailing newline --- individual-shell-tools/ls/script-01.sh | 3 +-- individual-shell-tools/ls/script-02.sh | 2 +- individual-shell-tools/ls/script-03.sh | 5 ++--- individual-shell-tools/ls/script-04.sh | 9 ++++----- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/individual-shell-tools/ls/script-01.sh b/individual-shell-tools/ls/script-01.sh index 0897042fa..c80ea38cf 100755 --- a/individual-shell-tools/ls/script-01.sh +++ b/individual-shell-tools/ls/script-01.sh @@ -13,6 +13,5 @@ fi # TODO: Write a command to list the files and folders in this directory. # The output should be a list of names including child-directory, script-01.sh, script-02.sh, and more. -# I use ls command to list the files and folders in this directory. +# ls with no arguments lists the contents of the current directory. ls - diff --git a/individual-shell-tools/ls/script-02.sh b/individual-shell-tools/ls/script-02.sh index 719c6eafe..a24ecb007 100755 --- a/individual-shell-tools/ls/script-02.sh +++ b/individual-shell-tools/ls/script-02.sh @@ -4,5 +4,5 @@ set -euo pipefail # TODO: Write a command which lists all of the files in the directory named child-directory. # The output should be a list of names: helper-1.txt, helper-2.txt, helper-3.txt. -# The command that lists all of the files in the directory named child-directory is ls child-directory +# Passing a directory name to ls lists the contents of that directory. ls child-directory diff --git a/individual-shell-tools/ls/script-03.sh b/individual-shell-tools/ls/script-03.sh index f519a299d..f9262f805 100755 --- a/individual-shell-tools/ls/script-03.sh +++ b/individual-shell-tools/ls/script-03.sh @@ -5,6 +5,5 @@ set -euo pipefail # TODO: Write a command which _recursively_ lists all of the files and folders in this directory _and_ all of the files inside those folders. # The output should be a list of names including: child-directory, script-01.sh, helper-1.txt (and more). # The formatting of the output doesn't matter. -# To list all the files and folders in this directory and all of the files inside those folders I use ls -R - -ls -R \ No newline at end of file +# -R (recursive) lists this directory and the contents of every subdirectory. +ls -R diff --git a/individual-shell-tools/ls/script-04.sh b/individual-shell-tools/ls/script-04.sh index 019c01ac3..4ab35db50 100755 --- a/individual-shell-tools/ls/script-04.sh +++ b/individual-shell-tools/ls/script-04.sh @@ -15,13 +15,12 @@ echo "First exercise (sorted newest to oldest):" # TODO: Write a command which lists the files in the child-directory directory, one per line, sorted so that the most recently modified file is first. # The output should be a list of names in this order, one per line: helper-3.txt, helper-1.txt, helper-2.txt. -# The command which lists the files in the child-directory directory, one per line, sorted so that the most recently modified file is first is ls -t child-directory -ls -t -1 child-directory +# -t sorts by modification time (newest first), -1 prints one name per line. +ls -t1 child-directory echo "Second exercise (sorted oldest to newest):" # TODO: Write a command which does the same as above, but sorted in the opposite order (oldest first). # The output should be a list of names in this order, one per line: helper-2.txt, helper-1.txt, helper-3.txt. -# The command which sorts in the opposite order (oldest first) is ls -tr -1 child-directory. - -ls -tr -1 child-directory +# -r reverses the sort order, so the oldest file comes first. +ls -tr1 child-directory From 91a7bcd499ffb3de9833d259edf989031b863815 Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Mon, 16 Nov 2026 14:05:29 +0000 Subject: [PATCH 06/11] Complete cat exercises with explanatory comments --- individual-shell-tools/cat/script-01.sh | 2 ++ individual-shell-tools/cat/script-02.sh | 4 ++++ individual-shell-tools/cat/script-03.sh | 3 +++ individual-shell-tools/cat/script-04-stretch.sh | 4 ++++ 4 files changed, 13 insertions(+) diff --git a/individual-shell-tools/cat/script-01.sh b/individual-shell-tools/cat/script-01.sh index c85053e0f..ec4a6d5ac 100755 --- a/individual-shell-tools/cat/script-01.sh +++ b/individual-shell-tools/cat/script-01.sh @@ -4,3 +4,5 @@ set -euo pipefail # TODO: Write a command to output the contents of the helper-1.txt file inside the helper-files directory to the terminal. # The output of this command should be "Once upon a time...". +# The path starts with .. because helper-files is one level up from this directory. +cat ../helper-files/helper-1.txt diff --git a/individual-shell-tools/cat/script-02.sh b/individual-shell-tools/cat/script-02.sh index 01bbd5eab..b49ad92ce 100755 --- a/individual-shell-tools/cat/script-02.sh +++ b/individual-shell-tools/cat/script-02.sh @@ -11,3 +11,7 @@ set -euo pipefail # It looked delicious. # I was tempted to take a bite of it. # But this seemed like a bad idea... + +# * matches every file in helper-files (in alphabetical order), so cat receives all three. +# Same as: cat ../helper-files/helper-1.txt ../helper-files/helper-2.txt ../helper-files/helper-3.txt +cat ../helper-files/* diff --git a/individual-shell-tools/cat/script-03.sh b/individual-shell-tools/cat/script-03.sh index 37573b0c1..0080e4ea0 100755 --- a/individual-shell-tools/cat/script-03.sh +++ b/individual-shell-tools/cat/script-03.sh @@ -9,3 +9,6 @@ set -euo pipefail # 1 It looked delicious. # 2 I was tempted to take a bite of it. # 3 But this seemed like a bad idea... + +# -n numbers each output line. +cat -n ../helper-files/helper-3.txt diff --git a/individual-shell-tools/cat/script-04-stretch.sh b/individual-shell-tools/cat/script-04-stretch.sh index 00fe3c48b..e52010c99 100755 --- a/individual-shell-tools/cat/script-04-stretch.sh +++ b/individual-shell-tools/cat/script-04-stretch.sh @@ -13,3 +13,7 @@ set -euo pipefail # 3 It looked delicious. # 4 I was tempted to take a bite of it. # 5 But this seemed like a bad idea... + +# With several files, cat -n keeps counting across them instead of restarting at 1. +# Alternative: cat ../helper-files/* | cat -n +cat -n ../helper-files/* From 72eb22a9ba2a5a17d78b39a93ab020ca78aee51a Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Mon, 16 Nov 2026 14:58:04 +0000 Subject: [PATCH 07/11] Complete grep exercises 1-2 with explanatory comments --- individual-shell-tools/grep/script-01.sh | 3 +++ individual-shell-tools/grep/script-02.sh | 3 +++ individual-shell-tools/wc/script-01.sh | 3 +++ individual-shell-tools/wc/script-02.sh | 3 +++ individual-shell-tools/wc/script-03.sh | 3 +++ 5 files changed, 15 insertions(+) diff --git a/individual-shell-tools/grep/script-01.sh b/individual-shell-tools/grep/script-01.sh index fb05f42f2..4642b21ca 100755 --- a/individual-shell-tools/grep/script-01.sh +++ b/individual-shell-tools/grep/script-01.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt said by the Doctor. # The output should contain 6 lines. + +# ^ means "starts with", so this only matches lines where the Doctor is speaking +grep "^Doctor:" dialogue.txt diff --git a/individual-shell-tools/grep/script-02.sh b/individual-shell-tools/grep/script-02.sh index df6f85640..2a5e25ce0 100755 --- a/individual-shell-tools/grep/script-02.sh +++ b/individual-shell-tools/grep/script-02.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that contains the word Doctor (regardless of case). # The output should contain 9 lines. + +# -i ignores case, so it matches Doctor, doctor, DOCTOR, etc. +grep -i Doctor dialogue.txt diff --git a/individual-shell-tools/wc/script-01.sh b/individual-shell-tools/wc/script-01.sh index c9dd6e5df..5e208de0b 100755 --- a/individual-shell-tools/wc/script-01.sh +++ b/individual-shell-tools/wc/script-01.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output the number of words in the file helper-files/helper-3.txt. # The output should include the number 19. The output should not include the number 92. + +# -w counts words only +wc -w ../helper-files/helper-3.txt diff --git a/individual-shell-tools/wc/script-02.sh b/individual-shell-tools/wc/script-02.sh index 8feeb1a62..c62d1351e 100755 --- a/individual-shell-tools/wc/script-02.sh +++ b/individual-shell-tools/wc/script-02.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output the number of lines in the file helper-files/helper-3.txt. # The output should include the number 3. The output should not include the number 19. + +# -l counts lines only +wc -l ../helper-files/helper-3.txt diff --git a/individual-shell-tools/wc/script-03.sh b/individual-shell-tools/wc/script-03.sh index 6b2e9d3d1..4d44a62ed 100755 --- a/individual-shell-tools/wc/script-03.sh +++ b/individual-shell-tools/wc/script-03.sh @@ -8,3 +8,6 @@ set -euo pipefail # 1 7 39 ../helper-files/helper-2.txt # 3 19 92 ../helper-files/helper-3.txt # 5 30 151 total + +# With no flags, wc shows lines, words and characters , * matches every file in the folder +wc ../helper-files/* From 7fedde355a4508406c839ae18d3218c2fad7093d Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Tue, 17 Nov 2026 11:19:05 +0000 Subject: [PATCH 08/11] Complete grep exercises 3-7 with explanatory comments --- individual-shell-tools/grep/script-03.sh | 3 +++ individual-shell-tools/grep/script-04.sh | 3 +++ individual-shell-tools/grep/script-05.sh | 3 +++ individual-shell-tools/grep/script-06.sh | 3 +++ individual-shell-tools/grep/script-07.sh | 3 +++ 5 files changed, 15 insertions(+) diff --git a/individual-shell-tools/grep/script-03.sh b/individual-shell-tools/grep/script-03.sh index 5383fe578..83671fe3c 100755 --- a/individual-shell-tools/grep/script-03.sh +++ b/individual-shell-tools/grep/script-03.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output the number of lines in dialogue.txt that contain the word Doctor (regardless of case). # The output should be exactly the number 9. + +# -i ignores case and -c counts the matching lines instead of printing them +grep -ic doctor dialogue.txt diff --git a/individual-shell-tools/grep/script-04.sh b/individual-shell-tools/grep/script-04.sh index 80ee04776..cab5e78cc 100755 --- a/individual-shell-tools/grep/script-04.sh +++ b/individual-shell-tools/grep/script-04.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that does not contain the word "Hello" (regardless of case). # The output should contain 10 lines. + +# -i ignores case and -v inverts the match, so only lines WITHOUT "hello" are printed +grep -iv hello dialogue.txt diff --git a/individual-shell-tools/grep/script-05.sh b/individual-shell-tools/grep/script-05.sh index 1eb538185..885185ea7 100755 --- a/individual-shell-tools/grep/script-05.sh +++ b/individual-shell-tools/grep/script-05.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that contains the string "cure", as well as the line before that line. # The output should contain two pairs of two lines of text (with a separator between them). + +# -B 1 also prints 1 line Before each match; grep puts -- between separate groups +grep -B 1 cure dialogue.txt diff --git a/individual-shell-tools/grep/script-06.sh b/individual-shell-tools/grep/script-06.sh index 5670e3b6c..6bb166061 100755 --- a/individual-shell-tools/grep/script-06.sh +++ b/individual-shell-tools/grep/script-06.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output the name of every `.txt` file in this directory which contains a line of dialogue said by the Doctor. # The output should contain two filenames. + +# -l prints only the names of files that contain a match, not the lines themselves +grep -l "^Doctor:" *.txt diff --git a/individual-shell-tools/grep/script-07.sh b/individual-shell-tools/grep/script-07.sh index 9670ebad9..ff532c164 100755 --- a/individual-shell-tools/grep/script-07.sh +++ b/individual-shell-tools/grep/script-07.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output, for each `.txt` file in this directory, how many lines of dialogue the Doctor has. # The output should show that dialogue.txt contains 6 lines, dialogue-2.txt contains 2, and dialogue-3.txt contains 0. + +# -c counts matching lines; with several files it prints filename:count for each +grep -c "^Doctor:" *.txt From 6f9d7624ac871285da5887aeab47b2fa6dfdae58 Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Tue, 17 Nov 2026 13:07:53 +0000 Subject: [PATCH 09/11] Complete sed exercises with explanatory comments --- individual-shell-tools/sed/script-01.sh | 3 +++ individual-shell-tools/sed/script-02.sh | 3 +++ individual-shell-tools/sed/script-03.sh | 3 +++ individual-shell-tools/sed/script-04.sh | 3 +++ individual-shell-tools/sed/script-05.sh | 5 +++++ individual-shell-tools/sed/script-06.sh | 4 ++++ 6 files changed, 21 insertions(+) diff --git a/individual-shell-tools/sed/script-01.sh b/individual-shell-tools/sed/script-01.sh index d592970fc..7778c2539 100755 --- a/individual-shell-tools/sed/script-01.sh +++ b/individual-shell-tools/sed/script-01.sh @@ -5,3 +5,6 @@ set -euo pipefail # TODO: Write a command to output input.txt with all occurrences of the letter `i` replaced with `I`. # The output should contain 11 lines. # The first line of the output should be: "ThIs Is a sample fIle for experImentIng wIth sed.". + +# s/i/I/ substitutes i with I, and the g flag replaces every match on a line (not just the first) +sed "s/i/I/g" input.txt diff --git a/individual-shell-tools/sed/script-02.sh b/individual-shell-tools/sed/script-02.sh index abdd64d06..c3855eba1 100755 --- a/individual-shell-tools/sed/script-02.sh +++ b/individual-shell-tools/sed/script-02.sh @@ -5,3 +5,6 @@ set -euo pipefail # TODO: Write a command to output input.txt with numbers removed. # The output should contain 11 lines. # Line 6 of the output should be " Alisha". + +# [0-9] matches any single digit; replacing it with nothing (//) deletes it, and g removes every digit on the line +sed "s/[0-9]//g" input.txt diff --git a/individual-shell-tools/sed/script-03.sh b/individual-shell-tools/sed/script-03.sh index dd284a296..4f2c92a11 100755 --- a/individual-shell-tools/sed/script-03.sh +++ b/individual-shell-tools/sed/script-03.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output input.txt removing any line which contains a number. # The output should contain 6 lines. + +# /[0-9]/ selects lines containing a digit and the d command deletes those lines from the output +sed "/[0-9]/d" input.txt diff --git a/individual-shell-tools/sed/script-04.sh b/individual-shell-tools/sed/script-04.sh index 0052ac6c4..bc2b29e65 100755 --- a/individual-shell-tools/sed/script-04.sh +++ b/individual-shell-tools/sed/script-04.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output input.txt replacing every occurrence of the string "We'll" with "We will". # The output should contain 11 lines. + +# Double quotes let us use the apostrophe in We'll without escaping it; g replaces every occurrence +sed "s/We'll/We will/g" input.txt diff --git a/individual-shell-tools/sed/script-05.sh b/individual-shell-tools/sed/script-05.sh index 2dcc91a0c..6c9c8700f 100755 --- a/individual-shell-tools/sed/script-05.sh +++ b/individual-shell-tools/sed/script-05.sh @@ -6,3 +6,8 @@ set -euo pipefail # If a line starts with a number and a space, make the line instead end with a space and the number. # So line 6 which currently reads "37 Alisha" should instead read "Alisha 37". # The output should contain 11 lines. + +# -E enables extended regex so ( ) create capture groups without backslashes +# ^([0-9]+) captures the number at the start of the line, (.*) captures the rest, +# and \2 \1 writes them back in swapped order with a space between +sed -E "s/^([0-9]+) (.*)/\2 \1/" input.txt diff --git a/individual-shell-tools/sed/script-06.sh b/individual-shell-tools/sed/script-06.sh index 0b9390170..eac0987e8 100755 --- a/individual-shell-tools/sed/script-06.sh +++ b/individual-shell-tools/sed/script-06.sh @@ -8,3 +8,7 @@ set -euo pipefail # The output should contain 11 lines. # Line 3 should be "It contains many lines, and there are some things you may want to do with each of them.". # Line 11 should be "We also should remember, when we go shopping, to get 4 items: oranges, cheese, bread, olives.". + +# , * matches a comma followed by zero or more spaces, so commas with or without a space +# are both replaced with exactly one comma and one space +sed "s/, */, /g" input.txt From 8928f8c201fdc1bfa1f4a91f2641be96efeabf0e Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Tue, 17 Nov 2026 13:49:57 +0000 Subject: [PATCH 10/11] Complete awk exercises 1-5 with explanatory comments --- individual-shell-tools/awk/script-01.sh | 3 +++ individual-shell-tools/awk/script-02.sh | 3 +++ individual-shell-tools/awk/script-03.sh | 3 +++ individual-shell-tools/awk/script-04.sh | 3 +++ individual-shell-tools/awk/script-05.sh | 3 +++ 5 files changed, 15 insertions(+) diff --git a/individual-shell-tools/awk/script-01.sh b/individual-shell-tools/awk/script-01.sh index 8db4390af..866aba4f1 100755 --- a/individual-shell-tools/awk/script-01.sh +++ b/individual-shell-tools/awk/script-01.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output just the names of each player in `scores-table.txt`. # Your output should contain 6 lines, each with just one word on it. + +# awk splits each line into fields on whitespace; $1 is the first field (the player's name) +awk '{print $1}' scores-table.txt diff --git a/individual-shell-tools/awk/script-02.sh b/individual-shell-tools/awk/script-02.sh index 5956be9bd..cfcd645f0 100755 --- a/individual-shell-tools/awk/script-02.sh +++ b/individual-shell-tools/awk/script-02.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output the names of each player, as well as their city. # Your output should contain 6 lines, each with two words on it, separated by a space. + +# $1 is the name and $2 is the city; the comma in print inserts a space between them +awk '{print $1, $2}' scores-table.txt diff --git a/individual-shell-tools/awk/script-03.sh b/individual-shell-tools/awk/script-03.sh index af7c6e8b9..65a48b95f 100755 --- a/individual-shell-tools/awk/script-03.sh +++ b/individual-shell-tools/awk/script-03.sh @@ -5,3 +5,6 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the score from their first attempt. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 1". + +# $3 is the third field, which is the score from the player's first attempt +awk '{print $1, $3}' scores-table.txt diff --git a/individual-shell-tools/awk/script-04.sh b/individual-shell-tools/awk/script-04.sh index bf15703c7..121cc875f 100755 --- a/individual-shell-tools/awk/script-04.sh +++ b/individual-shell-tools/awk/script-04.sh @@ -5,3 +5,6 @@ set -euo pipefail # TODO: Write a command to output just the names of each player in London along with the score from their last attempt. # Your output should contain 3 lines, each with one word and one number on it. # The first line should be "Ahmed 4". + +# the pattern $2 == "London" only runs the action on London lines; $NF is the last field (NF = number of fields), i.e. the last attempt's score +awk '$2 == "London" {print $1, $NF}' scores-table.txt diff --git a/individual-shell-tools/awk/script-05.sh b/individual-shell-tools/awk/script-05.sh index d1680cb02..a5e53b0a4 100755 --- a/individual-shell-tools/awk/script-05.sh +++ b/individual-shell-tools/awk/script-05.sh @@ -5,3 +5,6 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the number of times they've played the game. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 3". + +# NF is the number of fields on the line; subtracting 2 (name and city) leaves the number of scores, i.e. games played +awk '{print $1, NF - 2}' scores-table.txt From c6f2d6f8b60a655a8156a9701a632b766bfd661c Mon Sep 17 00:00:00 2001 From: shehu-dona Date: Tue, 17 Nov 2026 14:15:25 +0000 Subject: [PATCH 11/11] Complete awk stretch exercises with explanatory comments --- individual-shell-tools/awk/script-06-stretch.sh | 3 +++ individual-shell-tools/awk/script-07-stretch.sh | 3 +++ 2 files changed, 6 insertions(+) diff --git a/individual-shell-tools/awk/script-06-stretch.sh b/individual-shell-tools/awk/script-06-stretch.sh index 0201e6378..2a65a4c20 100755 --- a/individual-shell-tools/awk/script-06-stretch.sh +++ b/individual-shell-tools/awk/script-06-stretch.sh @@ -6,3 +6,6 @@ set -euo pipefail # TODO: Write a command to output the total of adding together all players' first scores. # Your output should be exactly the number 54. + +# total += $3 adds each player's first score as awk reads each line; the END block runs once after the last line and prints the sum +awk '{total += $3} END {print total}' scores-table.txt diff --git a/individual-shell-tools/awk/script-07-stretch.sh b/individual-shell-tools/awk/script-07-stretch.sh index 3f7155880..e8db7eb3a 100755 --- a/individual-shell-tools/awk/script-07-stretch.sh +++ b/individual-shell-tools/awk/script-07-stretch.sh @@ -7,3 +7,6 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the total of adding all of that player's scores. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 15". The second line should be "Basia 37" + +# for each line: reset total to 0, loop over fields 3 to NF (all the scores) adding each $i, then print the name and its total +awk '{total = 0; for (i = 3; i <= NF; i++) total += $i; print $1, total}' scores-table.txt