This topic contains 4 replies, has 2 voices, and was last updated by
February 10, 2017 at 2:21 pm #63929
I know there's Select-String, and -cmatch to find strings. My understanding is that Select-String is not very strict, and -cmatch is the best option to find exact matches since you can use regex.
My question is, does -cmatch have a .LineNumber option like Select-String?
February 10, 2017 at 2:44 pm #63931ParticipantPoints: 741Rank: Major Contributor
You can use regex with Select-String as well.
February 10, 2017 at 2:52 pm #63932
How would I do that?
I have a text file with these numbers, and I'm trying to find only 2=2
$TXT = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt' $Read = Get-Content $TXT $Read | Select-String -CaseSensitive "^2=2$" -SimpleMatch
If I do this, it will not show any results
But, when I have
$TXT = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt' $Read = Get-Content $TXT $Read -cmatch "^2=2$"
It will show only 2=2, which is what I'm looking for
How would I use regex on select-string?
February 10, 2017 at 3:00 pm #63935ParticipantPoints: 741Rank: Major Contributor
First of all: Did you at least try to get information you need by reading the help for the cmdlet you're about to use?
Get-Help Select-String -Full Get-Help Select-String -ShowWindow Get-Help Select-String -Online
On top of this there are thousands of examples on the internet.
Now to your request:
$TXT = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt' Select-String -Path $TXT -Pattern '^2=2$' -AllMatches | Select-Object *
Now you can choose whatever result you are looking for. But actually you don't need a regex for that. You are looking for a particular string!?
February 10, 2017 at 3:10 pm #63937
I did look at Get-Help Select-String -Examples
The topic ‘Finding string’ is closed to new replies.