简体   繁体   English

如何使用Powershell select-string -Allmatch选择多个列并扩展属性?

[英]How do I select multiple columns and expand properties using powershell select-string -Allmatch?

I'm searching a collection of text files (*.sql) for the occurrence of 8 digit numbers that begin with a 9. There could be multiple instances of these numbers in the file as well as multiple instances on each line in the file. 我正在搜索文本文件(* .sql)的集合,查找是否出现以9开头的8位数字。文件中可能存在这些数字的多个实例,并且文件的每一行上可能有多个实例。 I only want the output to display the unique occurrence of each 8 digit number in each file. 我只希望输出显示每个文件中每个8位数字的唯一出现。 This is what I have so far: 这是我到目前为止的内容:

Select-String "9[0-9]{8}" "*.sql" -AllMatches | Select-Object FileName, @{N="Value";E={ $_.matches |  %{$_.groups[0].value}}} | Select-Object  -unique FileName,Value  

And my output look like this: 我的输出如下所示:

FileName                       Value
--------                       -----

File1.sql                      907520714
File1.sql                      {907500507, 907520700, 907520701, 907520703...} 
File1.sql                      {907520725, 907520727, 907520728, 907520729} 
File1.sql                      990140600
File2.sql                      990319161
File2.sql                      {990603919, 990603925, 990603926} 
File2.sql                      {991100103, 991100103}
File2.sql                      {990700023, 990700504, 990700521, 990740520...} 
File3.sql                      907500044

etc.... 等等....

What I would like to do is expand the arrays so that when I pipe it through select -unique I actually will only get the unique 8 digit numbers contained in each file. 我想做的是扩展数组,以便当我通过select -unique传递它时,实际上我只会得到每个文件中包含的唯一8位数字。

This is what I would like: 这就是我想要的:

FileName                       Value
--------                       -----

File1.sql                      907520714
File1.sql                      907500507
File1.sql                      907520700
File1.sql                      907520701
File1.sql                      907520703 
File1.sql                      907520725
File1.sql                      907520727
File1.sql                      907520728
File1.sql                      907520729 
File1.sql                      990140600
File2.sql                      990319161
File2.sql                      990603919
File2.sql                      990603925
File2.sql                      990603926 
File2.sql                      991100103
File2.sql                      990700023
File2.sql                      990700504
File2.sql                      990700521
File2.sql                      990740520 
File3.sql                      907500044

etc... 等等...

How can I do this? 我怎样才能做到这一点? And can my current powershell command be improved? 我当前的powershell命令可以改进吗?

Thanks! 谢谢!

Ok, mostly running off what you have, kind of. 好吧,主要是跑掉您拥有的东西。 I'm doing a ForEach loop against all of the matches found by the Select-String, and grouping them by file name. 我正在对选择字符串找到的所有匹配项进行ForEach循环,然后按文件名将它们分组。 Then for each file I loop through the groups expanding Matches, and then take only unique values for the matched text. 然后,对于每个文件,我遍历展开“匹配”的组,然后仅对匹配的文本取唯一值。 For each value I output an object containing the name of the file, and the value that was matched. 对于每个值,我输出一个包含文件名和匹配值的对象。

ForEach($File in Select-String "9[0-9]{8}" "*.sql" -AllMatches | Group FileName){
    $File.group|select -expand matches|Select Value -Unique|%{
        new-object PSObject -property @{
            FileName=$File.Name
            Match=$_.value
        }
    }
}

I reformatted the command a bit, I think the terse nature of PowerShell shorthand makes it harder to see the problem. 我对命令进行了一些重新格式化,我认为PowerShell速记的简洁性质使其更难发现问题。

Select-String "9[0-9]{8}" "*.sql" -AllMatches | `
    Select-Object FileName, @{N="Value";E={ $_.matches | %{$_.groups[0].value}}} | `
    Select-Object  -unique FileName,Value 

Your original command yields one output line for each MatchInfo returned by Select-String. 您的原始命令为Select-String返回的每个MatchInfo产生一条输出行。 A MatchInfo represents a matching line of the text file. MatchInfo表示文本文件的匹配行。 I believe you are getting an array of values when a single line in the file contains more than one matching value. 我相信当文件中的一行包含多个匹配值时,您将获得一组值。

I revised it to clarify that we want one output object for each RegexMatch in each MatchInfo, which is to say one output object for each matching value. 我对其进行了修订,以阐明我们希望为每个MatchInfo中的每个RegexMatch提供一个输出对象,也就是说,为每个匹配值提供一个输出对象。

Select-String "9[0-9]{8}" "*.sql" -AllMatches | `
    Foreach-Object { 
        # $_ is MatchInfo for each matching line in file
        $fileName = $_.FileName 
        $_.Matches | Foreach-Object { 
            # $_ is RegexMatch for each match in line
            $_ | Select-Object -Property @{N="FileName";E={$fileName} },Value
        } 
    } | Select-Object  -unique FileName,Value 

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

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