Powershell提取字符串以

时间:2018-12-12 14:11:42

标签: powershell

我有一个包含以下内容的txt文件:

testdemo | 5 | 4563456| OMNI | retailomni
testRetail | 142 | 3453456345| testRetai111l | test
testtesttest | 44 | 4564356| apl | APL

我需要选择从“ testdemo”开始的整个字符串。在PS中如何做到这一点?

$operator = "testdemo"
$operatorcontent = Get-Content operators.txt | "Need to get whole string, starts from $operator"    

3 个答案:

答案 0 :(得分:3)

您可以使用Select-String cmdlet。

Get-Content operators.txt | Select-String -Pattern "testdemo"

有关Select-String-> Get-Help Select-String -Online

的更多信息

答案 1 :(得分:2)

    Get-Content operators.txt | Select-String -Pattern "testdemo"

将选择该行中是否包含 testdemo

如果您要选择以 testdemo

开头的行
    Get-Content operators.txt | Select-String -Pattern "^testdemo"

答案 2 :(得分:1)

Get-Content operators.txt | ForEach-Object {
    if($_ -match "testdemo"){
        $_
    }
}