I want to parse 3 pieces of information from the last bit of wget command output. For example:
2022-12-26 19:14:44 (13.7 Mb/s) - ‘somelibrary.min.js’ saved [1077022]
I was able to get the date/time since that is a fixed length. I am unable to extract the est. transfer speed (13/7) and file size (1077022) values.
STR="2022-12-26 19:14:44 (13.7 Mb/s) - ‘somelibrary.min.js’ saved [1077022]"
echo date/time is ${STR::19}
I imagine the remaining substring extractions will need to be done with the help of regular expressions, but I am unable to figure it out. Is there a viable path using only *nix utils like awk, sed, etc.?
I tried awk:
echo "(13.7 Mb/s)" | awk '$0 ~ /(.* Mb\/s)/ {print $1}'
But I am getting (13.7 instead of just the number.
You could do this with bash's regular expression matching, using ( ) in the RE to capture the relevant parts and ${BASH_REMATCH[n]} to get them:
str="2022-12-26 19:14:44 (13.7 Mb/s) - ‘somelibrary.min.js’ saved [1077022]"
pattern='([-0-9]+ [:0-9]+) \(([^)]+)\) .*\[([0-9]+)\]'
if [[ "$str" =~ $pattern ]]; then
echo "date/time is ${BASH_REMATCH[1]}"
echo "transfer speed is ${BASH_REMATCH[2]}"
echo "file size is ${BASH_REMATCH[3]}"
else
echo "The string is not in the expected format"
fi
BTW, I recommend using lower- or mixed-case variable names to avoid conflicts with the many all-caps names with special functions, and running your scripts through shellcheck.net to find common mistakes.
This awk should work for you:
s="2022-12-26 19:14:44 (13.7 Mb/s) - ‘somelibrary.min.js’ saved [1077022]"
awk -F '[][()[:blank:]]+' '{
printf "DateTime: %s %s, Speed: %s, Size: %s\n", $1, $2, $3, $(NF-1)
}' <<< "$s"
DateTime: 2022-12-26 19:14:44, Speed: 13.7, Size: 1077022
Breakdown:
-F '[][()[:blank:]]+' sets 1+ of [ or ] or ( or ) or a whitespace as input field separatorIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With