I've been out of programming for more than 10 years. I have a text file that I'm trying to read the Mpbs value for the upload and download speed.
Server: Iomart - Maidenhead (id = 3839) ISP: Plusnet Latency: 8.59 ms (0.10 ms jitter) Download: 71.43 Mbps (data used: 52.8 MB) Upload: 18.39 Mbps (data used: 16.9 MB) Packet Loss: 0.0%
I've been using a regex tool to test and build my regex, which delivers the result I want.
regex2="Upload:\W+([0-9]+\.[0-9]+)" regex3="Download:\W+([0-9]+\.[0-9]+)" while read line; do if [[ $line =~ regex2 ]] then echo "match ${BASH_REMATCH[1]}" UPS=${BASH_REMATCH[1]} fi if [[ $line =~ regex3 ]] then echo "match ${BASH_REMATCH[1]}" DNS=${BASH_REMATCH[1]} fi done < Result.txt
I'm sure there must be a better way to achieve what I want, but the main problem I'm having is the regex not working.
regex2
andregex3
, I think you want$regex2
and$regex3
.\W
is a PCRE feature -bash
only claims to support ERE. I suspect that (especially on MacOS) the bash version doesn't recognize\W
(or treats it as literalW
). You could consider using[[:blank:]]
instead to match horizontal whitespace (unless you specifically expect other non-word characters between the:
and the number)\W
is the same as POSIX[^[:alnum:]_]
.