1

I have a text file in the format as below:

Demo
1
2
3
[[1]]
[[2]]

In a Powershell script, I'm trying to find the first line number where the string matches [[.

$path = 'C:\Demo\Test.txt'
$pattern = "Demo"
$LineNumber = Select-String $path -Pattern $pattern | Select-Object LineNumber

The above code returns the line number 1 as @{LineNumber=1}

But I'm mainly interested to get the line number where it starts from [[. How do I achieve this, the script fails if I use [[ in the pattern.

Kindly help, thanks for all the suggestions.

1 Answer 1

1

The parameter -Pattern uses a regular expression by default. [ has a special meaning in regular expressions so needs to be escaped (using \):

Select-String input.txt -Pattern "\[\[" | Select-Object LineNumber

Alternatively pass the -SimpleMatch parameter to treat -Pattern as a string instead:

Select-String input.txt -Pattern "[[" -SimpleMatch | Select-Object LineNumber

Reference docs: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-6

Sign up to request clarification or add additional context in comments.

Comments

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.