How to make select-string only match records that are 6 characters long?

877 views Asked by At

I’m creating a script that reads a text file and compares the results to an array. It works fine, but I have some records that say they match but they don’t.

For example - TG1032 and TG match according to the select-string script.

Here is my select-string:

$Sel = select-string -pattern $strArrVal -path $txt

Is there a way to alter this to make select-string only match records that are 6 characters long?

1

There are 1 answers

0
Matt On

I would still like to point out where your pattern is wrong but the solution will most likely be the same regardless. If you are looking to match lines that are exactly 6 characters then you could just use the pattern ^.{6}$.

$strArrVal = "^.{6}$"
Select-String -Pattern $strArrVal -Path $txt

If that is really all you are looking for then regex is not really required. You could do this with Get-Content with similar results

Get-Content $txt | Where-Object{$_.length -eq 6}