【问题标题】:How to select strings from file via one lines?如何通过一行从文件中选择字符串?
【发布时间】:2023-02-10 20:54:41
【问题描述】:

如何通过一行从文件中选择字符串?
例如我的文件包含字符串
字符串 1
字符串 2
字符串 3
串4

我想要得到
字符串 2
串4

我这样试试

Get-Content -Path "E:\myfile.txt" | Select-String

但我不知道如何通过 Select-String 方法实现

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以使用 Where-Object cmdlet 来过滤对象流(在本例中为字符串):

    Get-Content -Path "E:myfile.txt" | Where-Object {$_ -match '[24]$'}
    # or
    Get-Content -Path "E:myfile.txt" | Where-Object {$_ -like '*[24]'}
    # or
    Get-Content -Path "E:myfile.txt" | Where-Object {$_.EndsWith('2') -or $_.EndsWith('4')'}
    

    【讨论】:

      【解决方案2】:

      获取内容路径“~Desktopstrings.txt” |选择字符串模式“string2|string4”

      【讨论】:

        猜你喜欢
        • 2018-04-04
        • 1970-01-01
        • 2010-12-03
        • 2011-09-19
        • 2021-08-07
        • 1970-01-01
        • 2015-02-14
        • 2022-08-17
        • 1970-01-01
        相关资源
        最近更新 更多