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
40 changes: 29 additions & 11 deletions cs205_final_exam.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
# TODO: Modify this file to create a shell script that is able to use awk to go through a file formatted like pokemon.dat and provides a printed report in the following format (where your script correctly calculates the values that go into the [VALUE] placeholders):
# ======= SUMMARY OF POKEMON.DAT ======
# Total Pokemon: [VALUE]
# Avg. HP: [VALUE]
# Avg. Attack: [VALUE]
# ======= END SUMMARY =======

# The "Avg." values should be calculated as mean values for the corresponding columns.
# The spacing and header formatting should match the above formatting description exactly.
# There should be a comment explaining the purpose of each line in your shell script.
# The data file will be passed in to the script as a positional parameter and will not necessarily be called pokemon.dat. However, you can assume that any file passed to this script will be formatted exactly the way pokemon.dat is formatted.
#!/bin/bash

# This script takes a file formatted like pokemon.dat as input and calculates summary statistics using awk.


# Get the filename from the first positional parameter

FILE=$1


# Use awk to calculate the total number of Pokemon and store it in a variable

total=$(awk 'END {print NR}' /home/grant/cs205/cs205_final/pokemon.dat)

# Use awk to calculate the average HP and store it in a variable

avg_hp=$(awk '{sum += $4} END {print sum / NR}' /home/grant/cs205/cs205_final/pokemon.dat)

# Use awk to calculate the average Attack and store it in a variable

avg_attack=$(awk '{sum += $5} END {print sum / NR}' /home/grant/cs205/cs205_final/pokemon.dat)

# Print the summary report

echo "======= SUMMARY OF POKEMON.DAT ======="
echo " Total Pokemon: $total"
echo " Avg. HP: $avg_hp"
echo " Avg. Attack: $avg_attack"
echo "======= END SUMMARY ======="
1 change: 1 addition & 0 deletions screenshots/cs205final
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@