I have a text file with the following contents:
something
another something
stuff
more stuff
Using PowerShell, I have a script that searches for the pattern "something". This pattern will appear at most once per line on the file. I am trying to determine the number of times that this search pattern was found in the file (i.e., the number of lines that contain this pattern). I am using the following script:
$something_list = Select-String -Path $some_path -Pattern "something" | Select-Object Line
I then run the following command to get the number of elements in the Line property:
$n = $something_list.Length - 1
The problem I'm having is that this works if there are 2+ instances of "something" in the file. If there is only 1 instance of "something" in the file, $something_list.Length is meaningless, since Length can't be referenced for Line objects with only 1 element in them.
How can I resolve this?