please help me I have result and it looks like this:`
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
--- 192.168.1.1 ping statistics ---
300 packets transmitted, 300 received, 0% packet loss, time 299426ms
rtt min/avg/max/mdev = 1.136/4.454/24.153/3.206 ms
`
I need search packet loss and I do:
ping -c 300 -q 192.168.1.1 | grep -oP '\d (?=% packet loss)'
and it's works but somtimes ping result look like this:
--- 192.168.1.1 ping statistics ---
300 packets transmitted, 299 received, 0,333333% packet loss, time 299433ms
rtt min/avg/max/mdev = 1.175/4.424/23.546/3.077 ms
then I have to use
ping -c 300 -q 192.168.1.1 | grep -oP '\d .\d (?=% packet loss)
'
Is it possible to combine these two queries to display the packet loss results?Because I don't know when the result will be in a fraction and when as an integer
CodePudding user response:
You could combine them as "digit or dot" . It is technically incorrect, but with ping it will do the job safely:
ping -c 300 -q 192.168.1.1 | grep -oP '[\d .,] (?=% packet loss)'
CodePudding user response:
You can modify your regular expression with an optional group after the \d
that matches the fraction:
\d (,\d )?
The grep
command becomes:
grep -oP '\d (,\d )?(?=% packet loss)'
Here comma is used as the decimal separator to match your example. If you want to match either comma or dot as suggested in a comment you can use the following fragment:
\d ([,.]\d )?
If you only want to match dot then you have to escape it with a backslash:
\d (\.\d )?