[英]How to match a specific string using -match in PowerShell
I have a text (.txt) file, example shown below:我有一个文本 (.txt) 文件,示例如下所示:
1 SHA1 <KEY1>
2 SHA1 <KEY2>
10 SHA1 <KEY3>
11 SHA1 <KEY4>
20 SHA1 <KEY5>
21 SHA1 <KEY6>
To get the required output, I use:要获得所需的 output,我使用:
$content = Get-Content -Path C:\Files\test.txt
$content -match "10 SHA1"
10 SHA1 <KEY3>
All fine so far.到目前为止一切都很好。
$content -match "20 SHA1"
20 SHA1 <KEY5>
Still the expected output, all good.还是预期的output,一切都好。
$content -match "1 SHA1"
1 SHA1 <KEY1>
11 SHA1 <KEY4>
21 SHA1 <KEY6>
Not good.不好。 Desired output:
所需的 output:
1 SHA1 <KEY1>
How can I get an exact match using -match
?如何使用
-match
获得完全匹配?
It matches all 3. If you use a start of line anchor, then it will work.它与所有 3 匹配。如果您使用行锚的开头,那么它将起作用。
@"
1 SHA1 <KEY1>
2 SHA1 <KEY2>
10 SHA1 <KEY3>
11 SHA1 <KEY4>
20 SHA1 <KEY5>
21 SHA1 <KEY6>
"@ -split '\r?\n' -match "^1 SHA1"
1 SHA1 <KEY1>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.