简体   繁体   English

在 powershell 中使用 select-string 后如何找到 substring?

[英]How do I find substring after using select-string in powershell?

How do I continue to get the 12345 as output?我如何继续获得 12345 作为 output?

file.txt文件.txt

MyText: 12345

myscript脚本

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.

相关问题 Powershell选择字符串子字符串 - Powershell select-string substring 如何使用 PowerShell 中的选择字符串查找字符串不匹配的文件? - How to find a file with not match string using select-string in PowerShell? 如何使用Powershell select-string -Allmatch选择多个列并扩展属性? - How do I select multiple columns and expand properties using powershell select-string -Allmatch? 如何在PowerShell中使用Select-String和regex匹配捕获值? - How do I capture a value using Select-String and regex matching in PowerShell? 如何使用PowerShell中的Get-Content和Select-String输出与'this_string'不匹配的行? - How do I output lines that do not match 'this_string' using Get-Content and Select-String in PowerShell? 当我在 PowerShell 中选择字符串(grep)时,如何仅返回匹配的正则表达式? - How do I return only the matching regular expression when I select-string(grep) in PowerShell? 如何将选择字符串转换为 pipe 到 substring - How to get select-string to pipe to substring 如何在 Powershell 中使用 select-string 进行多个管道 - How to do multiple piping with select-string in Powershell 如何拆分 powershell select-string 命令的结果? - How can I split the result of powershell select-string command? 如何使Powershell脚本横向压缩zip文件并基于select-string -pattern报告 - How do I make powershell script transverse zip files and report based off select-string -pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM