Looping through an array with -match operator

Hello,

My goal is to find the index of a line containing in substring in an array. And I would like to do this for multiple substrings.

The closest thing I could find would be to Loop through the array and test -match. This works fine when I explicitly put in the substring. But there are quite a few substrings I want to find so I tried to loop through an array:

[pre]

For ($j = 0; $j -lt $list.length; $j++) {
For ($i=0; $i -lt $file.length; $i++) {
$file[$i] -match ‘THH06121’ #this works just great
$file[$i] -match $list[$j] #this does not work - it just returns every single line as true
}
}

[/pre]

So $list[0] is THH06121, but it doesn’t seem to work. I’ve tried having quotes in the $list, I’ve tried having them in the body.

Hope you can help me! My end goal is to search for these substrings and replace the lines after them up to the next empty line.

Thank you,

Lora

I think you could achieve a little easier what you want like this:

$Array1 = @(
‘Robert’,
‘Roberta’,
‘Wiliam’,
‘William’,
‘Villiam’,
‘Lillian’
)
$Array2 = @(
‘bert’,
‘rob’,
‘liam’,
‘ili’,
‘lil’
)
foreach ($Item1 in $Array1){
foreach ($Item2 in $Array2) {
$Item1 -match $Item2
}
}

I gave this a little test run just to see if it’d work, and it does seem to.

# mockup for a file with a few lines in it
$File = @( "hello", "THH06121", "bye", "nope" )

# List of stuff to find
$List = @( "no", "THH06121" )

for ($listIndex = 0; $listIndex -lt $list.Length; $listIndex++) {
    for ($fileIndex = 0; $fileIndex -lt $file.Length; $fileIndex++) {
        if ($file[$fileIndex] -match $list[$listIndex]) { $fileIndex }
    }
}

The results are 3 and 1 which track with what I have going in as the input.

What’s actually in your list? You might need to escape the patterns you’re searching for depending on the characters they contain; -match uses regex, so if they contain characters considered “special” you’ll have to escape them. A good catch-all, if you know all the things you’re looking for are literal strings, is a nice easy [regex]::Escape($pattern)

Maybe you can do something with select-string.

PS C:\users\js> echo joe john james | select-string joe,john | fl


IgnoreCase : True
LineNumber : 1
Line       : joe
Filename   : InputStream
Path       : InputStream
Pattern    : joe
Context    :
Matches    : {0}

IgnoreCase : True
LineNumber : 2
Line       : john
Filename   : InputStream
Path       : InputStream
Pattern    : john
Context    :
Matches    : {0}

Oh, that’s a great idea! $File | Select-String -Pattern $list | Select-Object -Property LineNumber, Line should do the trick very nicely. :slight_smile:

Yep, or 2 patterns and 2 files:

PS C:\users\js> select-string joe,john text,text2

text:1:joe
text:2:john
text2:1:joe
text2:2:john

Thank you Olaf, the only problem, is now I do not know the index of that line.

Thank you Joel!

My $list is just a Get-Content drive from a text file:

[pre] $list = Get-Content \drive\file\folder\list.txt [/pre]

The text file just has very similar looking strings, about 8-10 characters, with only letters and numbers:

THH06121
THH60178

etc.

I want to find the indices of the lines in my $file so that I can replace the lines below them.

For example in my $file looks something like this:

[HKEY_LOCAL_MACHINE\SOFTWARE\TagInformation\TAH06121]
“Parameter1”=“1”
“Parameter2”=“PV”
“Display1”=“H1_2”
“Display2”=“H1_3”
“StateTxt”=“HI”
“BoxClr”="#FF0000"

[HKEY_LOCAL_MACHINE\SOFTWARE\TagInformation\TBH06121]
“Parameter1”=“1”
“Parameter2”=“PV”
“Display1”=“H1_2”
“Display2”=“H1_3”
“StateTxt”=“HI”
“BoxClr”="#FF0000"

[HKEY_LOCAL_MACHINE\SOFTWARE\TagInformation\THH06121]
“Parameter1”=“1”
“Parameter2”=“PV”
“Display1”=“H1_2”
“Display2”=“H1_3”
“StateTxt”=“HI”
“BoxClr”="#FF0000"

So if I find the line with THH06121, I want to change “StateTxt” to HIGH instead of HI.

I would do it on the regkey:

set-itemproperty HKLM:\SOFTWARE\TagInformation\TBH06121 StateTxt HIGH

Thank you js!

I didn’t realize I could just edit the registry directly.