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
52 changes: 40 additions & 12 deletions cs205_final_exam.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
# TODO: Modify this file to create a shell script that is able to use awk to go through a file formatted like best_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 DATA FILE =====
# File name: [VALUE]
# 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 best_pokemon.dat. However, you can assume that any file passed to this script will be formatted exactly the way best_pokemon.dat is formatted.
#!/bin/bash

# Ensure that a file path is provided as an argument
if [ $# -eq 0 ]; then
echo "Error: No input file provided."
echo "Usage: $0 <input_file>"
exit 1
fi

# Extract the input file path from command line arguments
input_file="$1"

# Calculate summary statistics using awk
summary=$(awk -F',' '
BEGIN {
total_pokemon = 0
total_hp = 0
total_attack = 0
}
NR > 1 {
total_pokemon++
total_hp += $2 # Assuming HP is in the second column
total_attack += $3 # Assuming Attack is in the third column
}
END {
avg_hp = (total_hp / total_pokemon)
avg_attack = (total_attack / total_pokemon)

# Print the summary report
printf("# ===== SUMMARY OF DATA FILE =====\n")
printf("# File name: %s\n", FILENAME)
printf("# Total Pokemon: %d\n", total_pokemon)
printf("# Avg. HP: %.2f\n", avg_hp)
printf("# Avg. Attack: %.2f\n", avg_attack)
printf("# ===== END SUMMARY =====\n")
}
' "$input_file")

# Print the summary
echo "$summary"
6 changes: 6 additions & 0 deletions screenshots/output.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ===== SUMMARY OF DATA FILE =====
# File name: best_pokemon.dat
# Total Pokemon: 703
# Avg. HP: 0.00
# Avg. Attack: 0.00
# ===== END SUMMARY =====