Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] About the best way to get information about the "matched" file #107

Closed
claudio-salvio opened this issue Jun 19, 2022 · 1 comment
Assignees
Labels
good first issue Good for newcomers question Further information is requested

Comments

@claudio-salvio
Copy link

claudio-salvio commented Jun 19, 2022

Hello,

When I pass files as parameters I would like the match to show also the information of the file.
For example if I execute:

Get-ChildItem -Path '.\' -Filter *.ps1|ForEach-Object{ $_|?<PowerShell_Requires> }

I get something like:

StartIndex EndIndex Value
---------- -------- -----
153 192 #requires -Version 2.0 -Modules ShowUI
0 23 #requires -Version 2.0
0 23 #requires -Version 1.0
0 23 #Requires -Version 3.0
24 59 #Requires -Modules ActiveDirectory

Checking the type I see that it is System.Text.RegularExpressions.Match.

To show which file the match corresponds to I used:

Get-ChildItem -Path '.\' -Filter *.ps1|ForEach-Object{ $_.FullName; $_|?<PowerShell_Requires> }

but honestly that's not what I would want to get.

Is there a "neater" way to get the match with the file info included?
For example some type like System.Text.RegularExpressions.FileMatch that in addition to StartIndex, EndIndex and Value contains the File System Object.

Best regards,
Claudio Salvio

@claudio-salvio claudio-salvio added the enhancement New feature or request label Jun 19, 2022
@StartAutomating StartAutomating added good first issue Good for newcomers question Further information is requested and removed enhancement New feature or request labels Jun 20, 2022
@StartAutomating
Copy link
Owner

@claudio-salvio thanks for asking this.

There are a couple of ways to get at the match information, and there's an easier way to handle files.

One of my favorites is -Extract, which will pull out each of the captures and make them into a PSObject.

Get-ChildItem -Filter *.ps1 -Recurse |  ?<PowerShell_Requires> -Extract

Using -IncludeInputObject will output the [IO.FileInfo] in the.InputObject property.

Get-ChildItem -Filter *.ps1 -Recurse | ?<PowerShell_Requires> -Extract -IncludeInputObject

Using -Include-Match will output the [Regex.Match] in the .Match property.

Get-ChildItem -Filter *.ps1 -Recurse | ?<PowerShell_Requires> -Extract -IncludeInputObject -IncludeMatch

And, for bonus points, Irregular augments matches in a number of ways. For instance, it adds a line and column

Get-ChildItem -Filter *.ps1 -Recurse |
    ?<PowerShell_Requires> -Extract -IncludeInputObject -IncludeMatch |
    Select-Object -ExpandProperty Match |
    Select-Object Line, Column

Please let me know if you have more questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants