Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions individual-shell-tools/awk/script-01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ 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 '{print $1}' scores-table.txt

# **How it works:**
# - `awk` processes the file line by line
# - `$1` refers to the first field (column) on each line, which is the player's name
# - This gives you 6 lines, one name each
10 changes: 10 additions & 0 deletions individual-shell-tools/awk/script-02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ 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.

awk '{print $1, $2}' scores-table.txt

# **How it works:**
# - `$1` = player name
# - `$2` = city
# - The `,` between them prints a **space** between the two values

#or you can also use,
#cut -d' ' -f1,2 scores-table.txt
8 changes: 8 additions & 0 deletions individual-shell-tools/awk/script-03.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ 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".

awk '{print $1, $3}' scores-table.txt

#how it works
#awk sees each word/number separated by spaces as a numbered field:
#awk reads the file line by line
# For each line, {print $1, $3} prints the 1st and 3rd fields
# The , between $1 and $3 adds a space between them
9 changes: 9 additions & 0 deletions individual-shell-tools/awk/script-04.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ 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".

awk '$2 == "London" {print $1, $NF}' scores-table.txt

# awk goes line by line and does two things:

# Filter — $2 == "London" skips any line where the 2nd field isn't London
# Print — for matching lines, print the name ($1) and the last score ($NF)

# $NF is the smart part — it automatically counts the fields on each line and picks the last one, so it doesn't matter that Leila has fewer scores than the others.
7 changes: 7 additions & 0 deletions individual-shell-tools/awk/script-05.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ 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".

awk '{print $1, NF-2}' scores-table.txt

# awk goes line by line and does two things:

# Print the name — $1 grabs the first field
# Count the scores — NF is the total number of fields, minus 2 removes the name and city, leaving just the number of scores
10 changes: 10 additions & 0 deletions individual-shell-tools/awk/script-06-stretch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ 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.


awk '{sum += $3} END {print sum}' scores-table.txt

# awk does two things:

# sum += $3 — goes line by line, continuously adding each player's first score ($3) to a running total
# END {print sum} — once all lines are done, prints the final total

# END is the key new concept — it's a special block that runs once after all lines have been read.
10 changes: 10 additions & 0 deletions individual-shell-tools/awk/script-07-stretch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ 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"


awk '{sum=0; for(i=3; i<=NF; i++) sum+=$i; print $1, sum}' scores-table.txt

# **How it works:**

# 1. **`sum=0`** — resets the total for each new player/line
# 2. **`for(i=3; i<=NF; i++)`** — loops through fields starting from `$3` (first score) up to `$NF` (last score), skipping name and city
# 3. **`sum+=$i`** — adds each score to the total
# 4. **`print $1, sum`** — prints the player name and their total
7 changes: 7 additions & 0 deletions individual-shell-tools/cat/script-01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ 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...".

cat ../helper-files/helper-1.txt



# cat reads a file and prints its contents to the terminal
# helper-files/helper-1.txt is the relative path — it looks for a folder called helper-files in your current directory, then the file inside it
8 changes: 8 additions & 0 deletions individual-shell-tools/cat/script-02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ set -euo pipefail
# It looked delicious.
# I was tempted to take a bite of it.
# But this seemed like a bad idea...


cat ../helper-files/*

# * is a wildcard that matches all files inside the helper-files directory
# So cat helper-files/* expands to all files at once, letting you call cat just once
# cat then prints them all in alphabetical file order

6 changes: 6 additions & 0 deletions individual-shell-tools/cat/script-03.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ 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...

cat -n ../helper-files/helper-3.txt

# How it works:

# cat -n is the same cat command but the -n flag adds line numbers to the output
7 changes: 7 additions & 0 deletions individual-shell-tools/cat/script-04-stretch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ 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...

cat -n ../helper-files/*

# How it works:

# cat -n with multiple files normally resets line numbers per file
# But with * wildcard, cat treats all files as one continuous stream, so line numbers keep incrementing across all files
7 changes: 7 additions & 0 deletions individual-shell-tools/grep/script-01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ set -euo pipefail

# TODO: Write a command to output every line in dialogue.txt said by the Doctor.
# The output should contain 6 lines.

grep "^Doctor:" dialogue.txt

# grep — scans files and returns lines that match a pattern
# -c — instead of printing matching lines, prints the count of matches per file
# ^Doctor: — the pattern; ^ means start of line, so only matches lines where Doctor: is at the beginning
# *.txt — runs across all .txt files in the current directory
10 changes: 10 additions & 0 deletions individual-shell-tools/grep/script-02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ 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.

grep -in "Doctor" dialogue.txt

# How it works:

# grep — scans files and returns lines that match a pattern
# -i — makes the search case-insensitive, matching Doctor, doctor, DOCTOR etc.
# -n — shows the line number of each matching line
# "Doctor" — the pattern to search for
# dialogue.txt — the file to search in
7 changes: 7 additions & 0 deletions individual-shell-tools/grep/script-03.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ 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.

grep -ic "Doctor" dialogue.txt

# How it works:

# -i makes the search case-insensitive
# -c instead of printing the matching lines, it prints the count of matching lines
7 changes: 7 additions & 0 deletions individual-shell-tools/grep/script-04.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ 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.

grep -iv "Hello" dialogue.txt

# How it works:

# -i makes the search case-insensitive
# -v inverts the match — prints every line that does not contain the word "Hello"
9 changes: 9 additions & 0 deletions individual-shell-tools/grep/script-05.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ 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).


grep -iB1 "cure" dialogue.txt

# How it works:

# -i makes the search case-insensitive
# -B1 prints 1 line Before each matching line
# The separator -- is automatically added by grep between each pair of results
7 changes: 7 additions & 0 deletions individual-shell-tools/grep/script-06.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ 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.

grep -l "Doctor" *.txt

# How it works:

# -l instead of printing the matching lines, it prints only the filename of each file that contains a match
# *.txt searches through all .txt files in the current directory
9 changes: 9 additions & 0 deletions individual-shell-tools/grep/script-07.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ 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.


grep -c "^Doctor" *.txt

# How it works:

# ^Doctor — ^ means "start of line", so it only matches lines where Doctor appears at the beginning, filtering out lines where Doctor is just mentioned mid-sentence
# -c — prints the count of matching lines per file
# *.txt — runs across all .txt files in the current directory
5 changes: 5 additions & 0 deletions individual-shell-tools/ls/script-01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ 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.


#-------answer--------------

# ls
7 changes: 7 additions & 0 deletions individual-shell-tools/ls/script-02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ 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.

#-------answer--------------

ls child-directory

# ls — lists files and folders
# child-directory — the folder to look inside
7 changes: 7 additions & 0 deletions individual-shell-tools/ls/script-03.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ 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.

#-------answer--------------

ls -R

# ls — lists files and folders
# -R — recursively lists all files and folders inside every subdirectory too
16 changes: 16 additions & 0 deletions individual-shell-tools/ls/script-04.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ 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.


#-------------answer--------------

# First exercise (sorted newest to oldest):
ls -t1 child-directory

# Second exercise (sorted oldest to newest):
ls -tr1 child-directory

# How it works:

# -t — sorts by modification time, newest first
# -r — reverses the order, making it oldest first
# -1 — displays one file per line
# child-directory — the folder to list
15 changes: 15 additions & 0 deletions individual-shell-tools/sed/script-01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ 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.".


#---------------answer--------------

sed 's/i/I/g' input.txt

# How it works:

# sed — stream editor that transforms text
# s/i/I/g — the substitution command:

# s — substitute
# i — the pattern to find
# I — the replacement
# g — globally replace all occurrences on each line, not just the first
12 changes: 12 additions & 0 deletions individual-shell-tools/sed/script-02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ 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".

#---------------answer--------------

sed 's/[0-9]//g' input.txt

# How it works:

# s/[0-9]//g — the substitution command:

# [0-9] — matches any digit (0 through 9)
# `` — replaces with nothing (deletes it)
# g — removes all occurrences on each line
10 changes: 10 additions & 0 deletions individual-shell-tools/sed/script-03.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ set -euo pipefail

# TODO: Write a command to output input.txt removing any line which contains a number.
# The output should contain 6 lines.


#---------------answer--------------

sed '/[0-9]/d' input.txt

# How it works:

# /[0-9]/ — matches any line containing a digit
# d — deletes the matching lines from the output
11 changes: 11 additions & 0 deletions individual-shell-tools/sed/script-04.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ 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.


#---------------answer--------------

sed "s/We'll/We will/g" input.txt

# How it works:

# sed — stream editor that transforms text line by line
# s/We'll/We will/g — substitute We'll with We will on every occurrence
# double quotes — used instead of single quotes because the pattern contains a '
14 changes: 14 additions & 0 deletions individual-shell-tools/sed/script-05.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ 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.


#---------------answer--------------

sed 's/^\([0-9]*\) \(.*\)/\2 \1/' input.txt

# How it works:

# sed — stream editor that transforms text line by line
# ^ — matches the start of the line
# \([0-9]*\) — captures the number as group 1
# — matches the space between number and name
# \(.*\) — captures the rest of the line as group 2
# \2 \1 — prints group 2 (name) first, then group 1 (number), swapping them
13 changes: 13 additions & 0 deletions individual-shell-tools/sed/script-06.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@ 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.".


#---------------answer--------------

sed 's/,\([^ ]\)/, \1/g' input.txt

# How it works:

# sed — stream editor that transforms text line by line
# , — matches a comma
# \([^ ]\) — captures any character that is not a space after the comma as group 1
# , \1 — replaces with a comma, a space, then group 1
# g — applies to all occurrences on each line
Loading