示例来自创建的iPer3.txt文件。
和0.00-30.00秒1.09 GBytes 312 Mbit/秒发送者
总和0.00-30.00秒1.09 GBytes 312 Mbit/秒接收机
我希望能够“提取”312 Mbit/秒-text,而其他的则不存在。之前,我曾使用这一行代码在PS-脚本完成后“呈现”iPerf-快速测试中的数字,但是整行显示与上面的示例类似。
Get-Content -Path $iperf3.txt | Where-Object {$_ -like'*[SUM]*0.00-15.00*'}
我一直在看这篇文章Get filtered content from text file with Get-Content,我想知道这是否是我想做的事。
我希望最终的结果是这样的:
您的速度测试结果是123 Mbit/秒下载。
你的速度测试结果是321兆位/秒上传。
如有任何意见或反馈,将不胜感激。谢谢!
EDITED/UPDATED
文本文件中的更多文本。
间隔传输带宽Retr
50.00-30.00秒116 MBytes 32.5Mbit/秒91发送者
50.00-30.00秒116 MBytes 32.3 Mbit/秒接收机
70.00-30.00秒116 MBytes 32.6 Mbit/秒94发送者
70.00-30.00秒116 MBytes 32.4 Mbit/秒接收机
9 0.00-30.00秒109 MBytes 30.5 Mbit/秒107发送者
9 0.00-30.00秒108 MBytes 30.2 Mbit/秒接收机
110.00-30.00秒120 MBytes 33.5Mbit/秒98发送者
110.00-30.00秒119 MBytes 33.3Mbit/秒接收机
130.00-30.00秒108 MBytes 30.2 Mbit/秒101发送者
130.00-30.00秒107 MBytes 29.9 Mbit/秒接收机
150.00-30.00秒123 MBytes 34.3 Mbit/秒104发送者
150.00-30.00秒122 MBytes 34.0Mbit/秒接收机
170.00-30.00秒102 MBytes 28.5 Mbit/秒104发送者
170.00-30.00秒101 MBytes 28.3 Mbit/秒接收机
19 0.00-30.00秒108 MBytes 30.2 Mbit/秒108发送者
190.00-30.00秒107 MBytes 30.0Mbit/秒接收机
210.00-30.00秒103 MBytes 28.8 Mbit/秒105发送者
210.00-30.00秒102 MBytes 28.6 Mbit/秒接收机
230.00-30.00秒125 MBytes 34.9Mbit/秒96发送者
230.00-30.00秒124 MBytes 34.6 Mbit/秒接收机
总和0.00-30.00秒1.10 GBytes 316 Mbit/秒1008发送者
总和0.00-30.00秒1.10 GBytes 314 Mbit/秒接收机
对于每个快速测试,iPerf将将上述内容添加到文本文件中,因此理论上可以添加两到三次。希望这能有所帮助。
发布于 2022-03-12 10:05:59
不知道该文件中是否有更多文本,也不知道它可能有多大,这里有两个选项供您选择:
Get-Content -Path 'D:\Test\iperf3.txt'|
Where-Object { $_ -match '\[SUM].*\s(\d+ Mbits/sec) (\w+)$'} |
ForEach-Object {
# define the text. if 'sender' then 'upload', otherwise 'download'
$updown = if ($matches[2] -eq 'sender') { 'upload' } else { 'download' }
'Your speedtest result is {0} for {1}' -f $matches[1], $updown
}如果文件很大,这将工作得更快,占用的内存也更少:
switch -Regex -File 'D:\Test\iperf3.txt' {
'\[SUM].*\s(\d+ Mbits/sec) (\w+)$' {
# define the text. if 'sender' then 'upload', otherwise 'download'
$updown = if ($matches[2] -eq 'sender') { 'upload' } else { 'download' }
'Your speedtest result is {0} for {1}' -f $matches[1], $updown
}
}Regex详细信息:
\[ Match the character “[” literally
SUM] Match the characters “SUM]” literally
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
( Match the regular expression below and capture its match into backreference number 1
\d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\ Mbits/sec Match the characters “ Mbits/sec” literally
)
\ Match the character “ ” literally
( Match the regular expression below and capture its match into backreference number 2
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
$ Assert position at the end of the string (or before the line break at the end of the string, if any)现在您已经显示了更多的文件,我注意到sender行在单词sender前面都有一个额外的数字值,在前面的示例中没有这个值。
因此,我们需要调整正则表达式。
这个新代码应该有十个工作:
switch -Regex -File 'D:\Test\iperf3.txt' {
'\[SUM].*\s(\d+ Mbits/sec)\s(?:[^\s]+\s)?(\w+)$' {
# define the text. if 'sender' then 'upload', otherwise 'download'
$updown = if ($matches[2] -eq 'sender') { 'upload' } else { 'download' }
'Your speedtest result is {0} for {1}' -f $matches[1], $updown
}
}Regex详细信息:
\[ Match the character “[” literally
SUM] Match the characters “SUM]” literally
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
( Match the regular expression below and capture its match into backreference number 1
\d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\ Mbits/sec Match the characters “ Mbits/sec” literally
)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
(?: Match the regular expression below
[^\s] Match any character that is NOT a “A whitespace character (spaces, tabs, line breaks, etc.)”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
)? Between zero and one times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
$ Assert position at the end of the string (or before the line break at the end of the string, if any)https://stackoverflow.com/questions/71448132
复制相似问题