Skip to content
Open
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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
2 changes: 2 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,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
4 changes: 4 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,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/*
3 changes: 3 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,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
4 changes: 4 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,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/*
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
2 changes: 2 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,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.
# ls with no arguments lists the contents of the current directory.
ls
2 changes: 2 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,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.
# Passing a directory name to ls lists the contents of that directory.
ls child-directory
2 changes: 2 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,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.
# -R (recursive) lists this directory and the contents of every subdirectory.
ls -R
5 changes: 4 additions & 1 deletion individual-shell-tools/ls/script-04.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +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.

# -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.
# -r reverses the sort order, so the oldest file comes first.
ls -tr1 child-directory
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
3 changes: 3 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,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
5 changes: 5 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,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
4 changes: 4 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,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
3 changes: 3 additions & 0 deletions individual-shell-tools/wc/script-01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions individual-shell-tools/wc/script-02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions individual-shell-tools/wc/script-03.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Loading