Find all rows longer than N

This is my first question. I am just starting out with Powershell.

Say I have a text file, and I want to combine only lines longer than 10 characters. So I made a very simple regex.

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

And I use the notmatch operator.

 $myTextFile | Select-String -NotMatch $reg 

This does not work. I also tried

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

but it doesn’t work either.

Any clue for me? Many thanks!

+4
source share
1 answer

You do not need regex. Just do the following:

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

If you want to do this with a regular expression, the dot matches any character. This will work ...

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

... but it's easier:

 Get-Content $myTextFile | Select-String '.{11,}' 
+6
source

Source: https://habr.com/ru/post/1496253/


All Articles