Skip to content

Commit 323f520

Browse files
awk exercises complete
1 parent c35ce92 commit 323f520

File tree

7 files changed

+16
-0
lines changed

7 files changed

+16
-0
lines changed

individual-shell-tools/awk/script-01.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
set -euo pipefail
44

55
# TODO: Write a command to output just the names of each player in `scores-table.txt`.
6+
awk '{ print $1}' scores-table.txt
7+
68
# Your output should contain 6 lines, each with just one word on it.

individual-shell-tools/awk/script-02.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
set -euo pipefail
44

55
# TODO: Write a command to output the names of each player, as well as their city.
6+
awk '{ print $1, $2}' scores-table.txt
67
# Your output should contain 6 lines, each with two words on it, separated by a space.

individual-shell-tools/awk/script-03.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
set -euo pipefail
44

55
# TODO: Write a command to output just the names of each player along with the score from their first attempt.
6+
awk '{ print $1, $3}' scores-table.txt
7+
68
# Your output should contain 6 lines, each with one word and one number on it.
79
# The first line should be "Ahmed 1".

individual-shell-tools/awk/script-04.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
set -euo pipefail
44

55
# TODO: Write a command to output just the names of each player in London along with the score from their last attempt.
6+
awk '$2 == "London" {print $1, $NF}' scores.txt
67
# Your output should contain 3 lines, each with one word and one number on it.
78
# The first line should be "Ahmed 4".

individual-shell-tools/awk/script-05.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
set -euo pipefail
44

55
# TODO: Write a command to output just the names of each player along with the number of times they've played the game.
6+
awk '{print $1, NF - 2}' scores-table.txt
67
# Your output should contain 6 lines, each with one word and one number on it.
78
# The first line should be "Ahmed 3".

individual-shell-tools/awk/script-06-stretch.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ set -euo pipefail
55
# NOTE: This is a stretch exercise - it is optional.
66

77
# TODO: Write a command to output the total of adding together all players' first scores.
8+
awk '{if (NF > 2) sum += $3} END {print sum}' scores.txt
9+
810
# Your output should be exactly the number 54.

individual-shell-tools/awk/script-07-stretch.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@ set -euo pipefail
55
# NOTE: This is a stretch exercise - it is optional.
66

77
# TODO: Write a command to output just the names of each player along with the total of adding all of that player's scores.
8+
9+
awk '{
10+
total = 0;
11+
for (i = 3; i <= NF; i++) total += $i;
12+
print $1, total
13+
}' scores-table.txt
14+
815
# Your output should contain 6 lines, each with one word and one number on it.
916
# The first line should be "Ahmed 15". The second line should be "Basia 37"

0 commit comments

Comments
 (0)