핑 결과를 간단한 형식(예: "10ms")으로 인쇄합니다.

핑 결과를 간단한 형식(예: "10ms")으로 인쇄합니다.

"ping -c 1 www.google.com"을 실행할 때마다 다음과 같은 결과가 나타납니다.

PING www.google.com (xxx.xx.xxx.xxx) 56(84) bytes of data.
64 bytes from xxxxxxxx-xx-xxxxxxxxxx.net (xxx.xx.xxx.xxx): icmp_seq=1 ttl=56 
time=25.8 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 25.816/25.816/25.816/0.000 ms

그러나 다음과 같은 간단한 형식으로 ping을 인쇄하고 싶습니다.

[23:00:25] 25.8 ms

이 목표를 어떻게 달성할 수 있나요? 지금까지 나는 시도했다

ping -c 1 www.google.com | grep -oP '(?<=time\s/)w+' > ping.txt

시간 없이 ping을 인쇄하지만 짐작할 수 있듯이 작동하지 않습니다.

답변1

노력하다

ping -c 1 www.google.com | awk 'NR==2{print strftime("[%T]",systime()),substr($8,6),"ms"}'

현재 시간을 가져와 systime()%T 표현(hh:mm:ss) 형식으로 지정하는 데 사용됩니다 strftime. 그런 다음 필드 8의 관련 부분을 인쇄하여 time=말도 안되는 부분을 제거합니다.

ping -c 1 www.google.com | awk 'NR==2{print strftime("[%T]",systime()),substr($8,6),"ms"}'
[21:32:05] 1.03 ms

답변2

다른방법:

ping -c1 www.google.com | awk -F'=' 'NR==2{ print strftime("[%T]",systime()),$NF; exit}'
[01:35:47] 41.5 ms

  • -F'='- 사용자 정의 필드 구분 기호

  • $NF- 마지막 필드 값

  • exit- 두 번째 레코드에 대한 입력 처리를 즉시 중지합니다.

관련 정보