Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all lines with a length greater than N

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.

$reg = "^\w{0,10}$"

And I use the notmatch operator.

$myTextFile | Select-String -NotMatch $reg

This is not working. I also tried

$reg = "^[a-zA-Z0-9]{0,10}$"

but this is not working either.

Any clue for me? Thanks a lot!

like image 577
Maquisard Avatar asked Aug 09 '13 22:08

Maquisard


1 Answers

You don't need a regex match. Just do this:

Get-Content $myTextFile | ?{$_.Length -gt 10}

If you want to do it with a regex, the dot matches any character. This will work...

Get-Content $myTextFile | Select-String -NotMatch '^.{0,10}$'

...but this is simpler:

Get-Content $myTextFile | Select-String '.{11,}'
like image 78
Adi Inbar Avatar answered Oct 13 '22 13:10

Adi Inbar