2

I want to find a command in windows equivalent to the following command in linux:

cat file.txt | grep -oP '\d{5}-\d{5}'

Out

99527-86793

with:

findstr /R "pattern" file.txt 

I try this but it works partially.

findstr [0-9]-[0-9] file.txt

Out:

de4f2114-847c-4cf2-8a0b-a38d04c0fdac1/1
99527-86793

How can I delimit the output to only those numbers: start and end 5 numbers 0 to 9 - 5 numbers 0 to 9 ?

thk

2 Answers 2

3

Something like this will do the trick:

findstr [0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9] file.txt

You may also be interested in grep for windows.

1

findstr doesn't extract matches. It only returns the string it finds. I recommend using powershell when dealing with regex. You can do something like:

select-string -Path 'c:\input.txt -Pattern '\d{5}-\d{5}' -AllMatches | % { $_.Matches } | % { $_.Value } > 'c:\output.txt'

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7

EDIT: I was incorrect that findstr cannot return matches but I still recommend using powershell for regex.

2
  • thanks for your command, but I prefer to do it in cmd - batch
    – acgbox
    Mar 9, 2020 at 16:51
  • in that case I recommend iskyfire answer above.
    – GSerrano
    Mar 9, 2020 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.