Question:
This is my first question here. I am just beginning with Powershell.
Let’s say I have a text file and I want to match only the lines with a length greater than 10 characters. So I made a really simple regex.
1 2 |
$reg = "^\w{0,10}$" |
And I use the notmatch operator.
1 2 |
$myTextFile | Select-String -NotMatch $reg |
This is not working. I also tried
1 2 |
$reg = "^[a-zA-Z0-9]{0,10}$" |
but this is not working either.
Any clue for me? Thanks a lot!
Answer:
You don’t need a regex match. Just do this:
1 2 |
Get-Content $myTextFile | ?{$_.Length -gt 10} |
If you want to do it with a regex, the dot matches any character. This will work…
1 2 3 4 5 6 |
Get-Content $myTextFile | Select-String -NotMatch '^.{0,10} ...but this is simpler:
|
Source:
Find all lines with a length greater than N by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
…but this is simpler:
1 |