简体   繁体   English

获取选择字符串输出的子字符串的最佳方法是什么?

[英]What is the best way to get a substring of Select-String output?

I am running Select-String on a file which contains the following line: 我正在包含以下行的文件上运行Select-String

....
Association ID: uj8da-21
....

Select-String -Path "..path.." -Pattern "Association ID:" -SimpleMatch gives me the file, the line number, the words "Association ID:", etc. All I want is the value. Select-String -Path "..path.." -Pattern "Association ID:" -SimpleMatch为我提供了文件,行号,单词“ Association ID:”等。我所需要的只是值。 What's the best way to do this from the Select-String command? 从“ Select-String命令执行此操作的最佳方法是什么?

And what if I wanted to extract a series of lines after this line (say, 10 additional lines) -- would the -Context argument be the way to go? 如果我想此行之后提取一系列行(例如,再添加10行),该-Context办? -Context参数是否可以解决?

Use match groups. 使用匹配组。

$matchInfo = Select-String -Path "C:\temp\text.txt" -Pattern "Association ID: (.*)" 
$matchInfo.Matches.Groups[1].Value

Context is the way to go. 上下文是必经之路。

$matchInfo = Select-String -Path "C:\temp\text.txt" -Pattern "Association ID: (.*)" -Context 0,10
$matchInfo.Matches.Groups[1].Value
$matchInfo.Context.PostContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM