bash ping 脚本和日志输出并用 grep 剪切
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11880511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ping script and log output and cut with grep
提问by Aaron
I want to ping a bunch of locations but not at the same time, in order so they don't timeout.
我想 ping 一堆位置,但不是同时 ping,以便它们不会超时。
The input is for example: ping google.com -n 10 | grep Minimum >> output.txt
输入例如: ping google.com -n 10 | grep Minimum >> output.txt
This will make the output of: Minimum = 29ms, Maximum = 46ms, Average = 33ms
这将使输出: Minimum = 29ms, Maximum = 46ms, Average = 33ms
But there are extra spaces in front of it which I don't know how to cut off, and when it outputs to the txt file it doesn't go to a new line. What I am trying to do is make it so I can copy and paste the input and ping a bunch of places once the previous finishes and log it in a .txt file and number them so it would look like:
但是它前面有多余的空格,我不知道如何截断,当它输出到txt文件时,它不会换行。我正在尝试做的是使它这样我可以复制和粘贴输入并在前一个完成后ping一堆地方并将其记录在一个.txt文件中并给它们编号,这样它看起来像:
Server 1: Minimum = 29ms, Maximum = 46ms, Average = 33ms
Server 2: Minimum = 29ms, Maximum = 46ms, Average = 33ms
Server 3: Minimum = 29ms, Maximum = 46ms, Average = 33ms
Server 4: Minimum = 29ms, Maximum = 46ms, Average = 33ms
回答by favoretti
Well, first of all, pingon linux limits packet number to send with -c, not -n.
Secondly, output of pingis not Minimum = xx ms, Maximum = yy ms, Avrage = zz ms, but rtt min/avg/max/mdev = 5.953/5.970/5.987/0.017 ms
好吧,首先,ping在 linux 上限制使用 发送的数据包数量-c,而不是-n。其次,输出ping不是Minimum = xx ms, Maximum = yy ms, Avrage = zz ms,而是rtt min/avg/max/mdev = 5.953/5.970/5.987/0.017 ms
So basically if you do something in lines of:
所以基本上如果你做一些事情:
for server in google.com yahoo.com
do
rtt=`ping $server -c 2 | grep rtt`
echo "$server: $rtt" >> output.txt
done
You should achieve what you want.
你应该达到你想要的。
[edit]
[编辑]
If cygwin is your platform, the easiest way to strip the spaces would be either what people are suggesting, sed, or then just | awk '{print $1}', will trim your line as well.
如果 cygwin 是您的平台,那么去除空格的最简单方法要么是人们建议的sed,要么只是| awk '{print $1}',也会修剪您的线条。
回答by Sicco
I think you might be able to solve this using sedtwo times and a whileloop at the end:
我认为您可以使用sed两次并while在最后使用循环来解决此问题:
N=1; ping google.com -n 10 | grep Minimum | sed -r 's/(Average = [[:digit:]]+ms)/\n/g' | sed -r s'/[[:space:]]+(Minimum)//g' | while read file; do echo Server "$N": "$file"; N=$((N+1)); done >> output.txt
The steps:
步骤:
- The first
sedfixes the newline issue:- Match the final part of the string after which you want a new line, in this case
Average = [[:digit:]]+msand put it into a group using the parenthesis - Then replace it with the same group (
\1) and insert a newline character (\n) after it
- Match the final part of the string after which you want a new line, in this case
- The second
sedremoves the whitespaces, by matching the wordMinimumand all whitespaces in front of it after which it only returns the wordMinimum - The final
whilestatement loops over each line and addsServer "$N":in front of the ping results. The$Nwas initialized to1at the start, and is increased with1after each read line
- 第一个
sed修复了换行问题:- 匹配您想要换行的字符串的最后部分,在这种情况下,
Average = [[:digit:]]+ms并使用括号将其放入一个组中 - 然后将其替换为相同的组 (
\1) 并在其后插入换行符 (\n)
- 匹配您想要换行的字符串的最后部分,在这种情况下,
- 第二个
sed删除空格,通过匹配单词Minimum和它前面的所有空格,之后它只返回单词Minimum - 最后的
while语句遍历每一行并添加Server "$N":到 ping 结果之前。将$N被初始化为1在一开始,并会增加1每次读取行后
回答by Rohan
You can use sed to remove first 4 spaces :
您可以使用 sed 删除前 4 个空格:
ping google.com -n 10 | grep Minimum | sed s/^\ \ \ \ //

