Findstr - returns only regular expression

I have this line in a text file ( test.txt):

BLA BLA BLA
BLA BLA
Found 11 errors and 7 warnings

I execute this command:

findstr /r "[0-9]+ errors" test.txt

To get only a string 11 errors.

Instead, the output is:

Found 11 errors and 7 warnings

Can anyone help?

+4
source share
2 answers

The findstr tool cannot be used to extract matches. Powershell is much easier to use for this.

Here is an example:

$input_path = 'c:\ps\in.txt'
$output_file = 'c:\ps\out.txt'
$regex = '[0-9]+ errors'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

See the Windows PowerShell article: Retrieving Strings Using Regular Expressions on how to use the script above.

+1
source

findstr , , . , . , findstr, :

findstr , , [0-9]+, - error. Found 11 errors and 7 warnings - error, , findstr + ( ), [0-9][0-9]*, . , /C; , /R.

findstr /R /C:"[0-9][0-9]* errors" "test.txt"

, , x5 errorse; , \< ( ) \> ( ). ( , /C:" [0-9][0-9]* errors ", , .)

, :

findstr /R /C:"\<[0-9][0-9]* errors\>" "test.txt"

, :

Found 11 errors and 7 warnings

, 2 errors are enough 35 warnings but less than 3 errors, , , :

findstr /R /C:"^Found [0-9][0-9]* errors and [0-9][0-9]* warnings$" "test.txt"

, 11 errors, :

  • a for /F findstr :

    for /F "tokens=2-3 delims= " %%E in ('
        findstr/R /C:"\<[0-9][0-9]* errors\>" "test.txt"
    ') do echo(%%E %%F
    
  • :

    for /F "delims=" %%L in ('
        findstr /R /C:"\<[0-9][0-9]* errors\>" "test.txt"
    ') do set "LINE=%%L"
    set "LINE=%LINE:* =%"
    set "LINE=%LINE: and =" & rem "%"
    echo(%LINE%
    
+4

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


All Articles