【问题标题】:Powershell doesn't show result exactly (ip address)Powershell 没有准确显示结果(IP 地址)
【发布时间】: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 选项解决了

标签: powershell select-string


【解决方案1】:
get-content test2.log | Select-String "1\.1\.2\.1"

工作正常

$ip="1\.1\.2\.1"
get-content test2.log | Select-String $ip

【讨论】:

  • {$pinghostname = Get-Content $HOSTS |Select-String -Pattern "$ip" | %{($_ -split "tt")[1]} "1.1.2.1" 应该是 $ip 这意味着我不能使用正则表达式
猜你喜欢
  • 1970-01-01
  • 2012-12-16
  • 2017-07-02
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
相关资源
最近更新 更多