【发布时间】:2023-02-10 20:54:41
【问题描述】:
如何通过一行从文件中选择字符串?
例如我的文件包含字符串
字符串 1
字符串 2
字符串 3
串4
我想要得到
字符串 2
串4
我这样试试
Get-Content -Path "E:\myfile.txt" | Select-String
但我不知道如何通过 Select-String 方法实现
【问题讨论】:
标签: powershell
如何通过一行从文件中选择字符串?
例如我的文件包含字符串
字符串 1
字符串 2
字符串 3
串4
我想要得到
字符串 2
串4
我这样试试
Get-Content -Path "E:\myfile.txt" | Select-String
但我不知道如何通过 Select-String 方法实现
【问题讨论】:
标签: powershell
您可以使用 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')'}
【讨论】:
获取内容路径“~Desktopstrings.txt” |选择字符串模式“string2|string4”
【讨论】: