Discussion:
locate array index with a -match or regex.
(too old to reply)
Justin Rich
2010-02-11 19:55:55 UTC
Permalink
basically im trying to find out what line a -match is on.

i know i could do it with a for loop and check each line, but with
everything that PS can do i was wondering if there was some little trick for
it.

Thanks
Justin
Robert Robelo
2010-02-11 20:54:37 UTC
Permalink
Select-String returns <MatchInfo> objects, these have a LineNumber property that is 1-based. PowerShell arrays, like .Net's arrays, are 0-based. All you'd have to do is subtract 1 from MatchInfo.LineNumer to have the index for a 0-based array:

$array = 1, 2, 3, 4, 3, 4, 2, 1, 2, 3, 4
$index = $array | Select-String 3 | % {$_.LineNumber
RichS [MVP]
2010-02-11 21:05:01 UTC
Permalink
I've not seen it done
--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk
Post by Justin Rich
basically im trying to find out what line a -match is on.
i know i could do it with a for loop and check each line, but with
everything that PS can do i was wondering if there was some little trick for
it.
Thanks
Justin
.
Loading...