從文字檔案中提取時間

從文字檔案中提取時間

因此,我嘗試在 Linux 中編寫 shell 腳本,該腳本將從儲存在文字檔案中的網頁伺服器 ping 中提取往返時間。所以我基本上有一個文字檔案:

    PING e11699.b.akamaiedge.net (104.100.153.112) 56(84) bytes of data.
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=1 ttl=60 time=17.2ms
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=2 ttl=60 time=12.6ms
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=3 ttl=60 time=11.7ms
    ... (a bunch more ping responses here)
    --- e11699.b.akamaiedge.net ping statistics ---
    86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
    rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2

因此,我最終使用 sed 嘗試從文字檔案中僅提取 17.2、12.6、11.7 和更多次。下面是我的 sed 行:

    sed 's/.*(time=\([0-9]*\(\.[0-9]*\)\{0,1\}\) ms/\1/' pingoutput.txt | sort -n > sortedtime.txt 

此行成功地提取並排序了我需要的所有時間,但它還從我不需要的 ping 文字檔案中提取了幾行。它創建的文本文件如下所示:

--- e11699.b.akamaiedge.net ping statistics ---
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2
11.7
12.6
17.2
...
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms

如果無論如何可以避免提取文字檔案中不需要的“---e11699”到“pipe 2”和“86400 packet”到“86532481ms”行,我將非常感謝您的幫助!

答案1

我用了這個 sed:

sed -n 's/.*time=\([0-9].*\)ms.*/\1/p' times | sort -n

在您的範例文件 ( times) 上:

PING e11699.b.akamaiedge.net (104.100.153.112) 56(84) bytes of data.
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=1 ttl=60 time=17.2ms
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=2 ttl=60 time=12.6ms
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=3 ttl=60 time=11.7ms
... (a bunch more ping responses here)
--- e11699.b.akamaiedge.net ping statistics ---
86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2

我得到這個結果:

sed -n 's/.*time=\([0-9].*\)ms.*/\1/p' times | sort -n
11.7
12.6
17.2


我用這個-n開關去掉了不需要的線條。從man sed

-n By default, each line of input is echoed to the standard output after all of the commands have been applied to it. The -n option suppresses this behavior.

答案2

如果grep使用pcre正規表示式沒問題:

$ grep -oP 'time=\K[0-9.]+' ip.txt | sort -n
11.7
12.6
17.2
  • -o只列印匹配的模式
  • -P使用 PCRE 正規表示式
  • time=\K正向回顧,不是輸出的一部分
  • [0-9.]+一個或多個數字和.字符
  • sort -n按數字排序

單獨使用perl

$ perl -nle 'push(@a,/time=\K([0-9.]+)/g); END{ print foreach sort {$a <=> $b} @a }' ip.txt 
11.7
12.6
17.2
  • 這裡用匹配的模式填充一個數組,然後在最後列印出按數字排序的數組

答案3

grep 'time=' pingoutput.txt | awk '{print $8}'

相關內容