Skip to content

Instantly share code, notes, and snippets.

@mingalevme
Last active August 10, 2021 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingalevme/47fb8dab2f2a2a39a68dfc7957d7bc8b to your computer and use it in GitHub Desktop.
Save mingalevme/47fb8dab2f2a2a39a68dfc7957d7bc8b to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
filename="${1:--}"
if [ "$filename" = "-" ]; then
filename="/dev/stdin"
fi
grep_options=( -Po )
if [[ "$OSTYPE" == "darwin"* ]]; then
grep_options=( -Eo )
fi
first_line=""
last_line=""
count=0
sum=0
while read -r line; do
SIZE=$(echo "$line" | grep "${grep_options[@]}" "HTTP\/1.1\" \d+ \d+" | grep "${grep_options[@]}" "\d+$")
if [ $? -ne 0 ]; then
continue
fi
if [ -z "$first_line" ] ; then
first_line="$line"
fi
last_line="$line"
count=$(( count + 1 ))
sum=$(( sum + SIZE ))
done < "$filename"
printf "First row: %s\n" "$first_line"
printf "Last row: %s\n" "$last_line"
printf "count=%s\n" "$count"
printf "sum=%s\n" "$sum"
if [ $count -eq 0 ]; then
printf "AVG=0\n"
else
printf "AVG=%s\n" $(( sum/count ))
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment