1

I am trying to print out a line in a text file if it starts with any of the string in the array.

Here is a snippet of my code:

array = "test:", "test1:"
    if($currentline | Select-String $array) {
        Write-Output "Currentline: $currentline"
    }

My code is able to print lines in the text file if it has any of the strings in the array variable. But I only want to print lines if it starts with the string in array variable.

Sample of text file:
abcd-test: 123123
test: 1232
shouldnotprint: 1232

Output: 
abcd-test: 123123
test: 1232

Expected output:
test: 1232  

I have seen some questions asked on stackoverflow with the solution:

array = "test:", "test1:"
    if($currentline | Select-String -Pattern "^test:") {
        Write-Output "Currentline: $currentline"
    }

But in my case I am using an array variable instead of a string to select the content so I am stumped at this portion because it would not work. It will just now print anything.

Update: Thanks Theo for your answer! This is my code based on Theo's answer for reference

array = "test:", "test1:" 
$regex = '^({0})' -f (($array |ForEach-Object { [regex]::Escape($_) }) -join '|') 
Loop here:
   if($currentline -match $regex) {
       Write-Output "Currentline: $currentline"
   }

1 Answer 1

2

Using the Regex -match operator should do what you want:

$array = "test:", "test1:"

# create a regex string from the array.
# make sure all the items in the array have their special characters escaped for Regex
$regex = '^({0})' -f (($array | ForEach-Object { [regex]::Escape($_) }) -join '|')
# $regex will now be '^(test:|test1:)'. The '^' anchors the strings to the beginning of the line

# read the file and let only lines through that match $regex
Get-Content -Path 'D:\Test\test.txt' | Where-Object { $_ -match $regex }

Or, if the file to read is really huge, use a switch -Regex -File method like:

switch -Regex -File 'D:\Test\test.txt' {
    $regex { $_ }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Theo, Thanks for your answer. Just to understand your code, it is attempting to loop through the array so that we could escape special characters and append a "|" to join each items together. This output will then be stored in $regex where we will use "Where-Object" to match the current line to the regex statement generated. May I know what does the "-f" option at line 3 mean? And is this "[regex]::Escape($_)" Powershell's equivalent of calling out a function (In this case Regex.Escape)?
@pikachu -f is the string format operator. See Get-Help about_Operators. And, yes, [Regex]::Escape() Is calling .Net's System.Text.RegularExpressions.Regex.Escape() function. Strictly, [Regex] is a class name, :: is the static member operator,, and Escape() is the member function. You could call it as [System.Text.RegularExpressions.Regex]::Escape().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.