[英]How do I find substring after using select-string in powershell?
How do I continue to get the 12345 as output?我如何继续获得 12345 作为 output?
MyText: 12345
Get-Content file.txt | Select-String "MyText:" | Select-Object Line
Now I would like to save last five characters, how to I retreive them?现在我想保存最后五个字符,如何取回它们?
The Select-String cmdlet -Pattern
parameter expects a regex - so you can get your desired output by capturing the five digits. Select-String cmdlet
-Pattern
参数需要一个正则表达式- 因此您可以通过捕获五位数字来获得所需的 output。 Note that the Select-String
cmdlet also takes a -Path
parameter to retrieve the content of a file.请注意,
Select-String
cmdlet 还采用-Path
参数来检索文件的内容。 This means you can omit the first Get-Content
command:这意味着您可以省略第一个
Get-Content
命令:
(Select-String -Path .\file.txt -Pattern 'MyText: (\d{5})').Matches.Groups[1].Value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.