【发布时间】:2018-05-29 07:06:05
【问题描述】:
我尝试只获取1.1.2.1,但它也显示了其他ip
我的测试代码是
$reader = @([System.IO.File]::ReadAllLines("c:\test2.log")) |select-string "1.1.2.1"
$reader = [System.IO.File]::ReadAllLines("c:\test2.log") |select-string "1.1.2.1"
$reader = get-content c:\test2.log |select-string "1.1.2.1"
和
-
test2.log
10.1.101.1 test101 10.1.102.1 test102 121.132.252.1 test104 100.101.122.1 test105 1.1.1.1 test1 1.1.2.1 test2 -
结果
10.1.102.1 test102 100.101.122.1 test105 1.1.2.1 test2
我不知道为什么它显示test102,test105
请告诉我如何解决这个问题
我在Powershell v2,v3中试过这个
对不起,我忘了解释原始代码的一部分
{$pinghostname = Get-Content $HOSTS |Select-String -Pattern "$ip" | %{($_ -split "tt")[1]}
“1.1.2.1”应该是一个$ip
这意味着我不能使用正则表达式
【问题讨论】:
-
使用 Select-String -SimpleMatch 选项解决了