0

I just see some regex could not work in windows cmd because it doesn't support all regex commands. I've found a regex for my problem.

So now I need to use findstr with this regex: (?si)(?:^(?<!.)|\R{2}|^Account:(?-s:.*)\R)\K(?:(?!\R{2}).)*?(?-s:\bfifa\b.*\b20\b).*?(?=\R{2}|\z) , but windows command line can't find this, and actually skips.

  1. How can I use above regex, to find all matches from a txt file and output them to another txt file in a batch file?

This is how I used to do it with other simple regex, but not working now:

type %LogFILE% | findstr /i /r /c:"(?si)(?:^(?<!.)|\R{2}|^Account:(?-s:.*)\R)\K(?:(?!\R{2}).)*?(?-s:\bfifa\b.*\b20\b).*?(?=\R{2}|\z)" > %LogNEW%
  1. Can we use powershell commands in cmd? I tried to do but it closes my batch command (I don't know is this correct syntax or no):
powershell -command "& {Get-ChildItem -Path .\LogFILE.txt -r | Select-String "(?si)(?:^(?<!.)|\R{2}|^Account:(?-s:.*)\R)\K(?:(?!\R{2}).)*?(?-s:\bfifa\b.*\b20\b).*?(?=\R{2}|\z)" > LogNEW.txt}"

EDITED: I want to select hits including fifa (as string not a single word) from a txt file, and output them to another txt file (separate solutions for 1 and 2 pattern):

Pattern 1 Solved: powershell -command "& {Get-ChildItem .\LogFILE.txt -r | Get-Content -Raw | Foreach-Object { $_ -Split '(\r?\n)(?=(\r?\n)+)' } | Select-String 'fifa'} | Out-File -encoding Default LogNEW.txt"

1:

Region: AR
OnlineID: Atl_Tuc
---Start---
FIFA 18 Legacy Edition
---END---

Region: FR
OnlineID: jubtrrzz
---Start---
FIFA 19
Undertale
Pro Evolution Soccer™ 2018
---END---

Region: US
OnlineID: Cu128yi
---Start---
KINGDOM HEARTS HD 1.5 +2.5 ReMIX
---END---

Region: RO
OnlineID: Se116
---Start---
Real Farm
EA SPORTS™ FIFA 20
LittleBigPlanet™ 3
---END---

Region: US
OnlineID: CAJ5Y
---Start---
Madden NFL 18: G.O.A.T. Super Bowl Edition
---END---

Pattern 2 Solved: powershell -command "& {Get-ChildItem .\LogFILE.txt -r | Get-Content -Raw | Foreach-Object { $_ -Split '(?<=---END---\s*\n)\s*\n' } | Select-String 'fifa'} | Out-File -encoding Default LogNEW.txt"

2:

Language: pt-BR (Actually I need [Language:.*])

Region: AR
OnlineID: Atl_Tuc
---Start---
FIFA 18 Legacy Edition
---END---

Language: en-US

Region: FR
OnlineID: jubtrrzz
---Start---
FIFA 19
Undertale
Pro Evolution Soccer™ 2018
---END---

Language: en-GB

Region: US
OnlineID: Cu128yi
---Start---
KINGDOM HEARTS HD 1.5 +2.5 ReMIX
---END---

Language: pt-BR

Region: RO
OnlineID: Se116
---Start---
Real Farm
EA SPORTS™ FIFA 20
LittleBigPlanet™ 3
---END---

Language: es-MX

Region: US
OnlineID: CAJ5Y
---Start---
Madden NFL 18: G.O.A.T. Super Bowl Edition
---END---
12
  • 3
    Well, findstr just supports a tiny subset of regular expressions, no grouping ((/)/?/:), no quantifiers (+/{/}/?) other than *, no alteration (|), etc.… Commented Nov 15, 2020 at 4:04
  • 4
    why are you using findstr.exe - a command line utility - instead of Select-String - a PoSh cmdlet? Commented Nov 15, 2020 at 4:06
  • 1
    Leverage from PowerShell as you anyway already tried; I can't help you with the syntax though… Commented Nov 15, 2020 at 4:09
  • 3
    I think we dealing with an XY Problem here. Why are you so eager to use the old fashion and limited CMD with findstr? Commented Nov 15, 2020 at 7:52
  • 1
    @morez890 - good to know that you got it working. my problem was with your original statement - cmd.exe versus powershell.exe - and that has been solved. thank you for the feedback! [grin] Commented Nov 15, 2020 at 19:26

1 Answer 1

1

As per comment, this sounds like an XY Problem; You're so eager to resolve this your own way (trying to use a single regular expression in the old-fasion CMD with a limited FindStr command) that you probably lost the broader picture along with any other attempted solution.

For example:

In PowerShell

Get-ChildItem .\LogFILE.txt -r |
    Get-Content -Raw | Foreach-Object {
        $_ -Split '(?<=---END---\s*\n)\s*\n' # or: '(?<=Language:[\S\s]*\n)\s*\n'
    } | Select-String 'fifa'

enter image description here

Putting this in a CMD command:

powershell -command "& {Get-ChildItem .\LogFILE.txt -r | Get-Content -Raw | Foreach-Object { $_ -Split '(?<=---END---\s*\n)\s*\n' } | Select-String 'fifa'} > LogNEW.txt"
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah I thought using that regex is the easiest way to solve this problem, that was why I just skipped the main problem. Your answer worked and thanks very much. But in some conditions I have a string like: "Start + a line of writing + an empty line + my main selection + end of selection" like this demo (but this regex needs a little change to turn to this: (?si)(?:^(?<!.)|\R{2}|^Account:(?-s:.*)\R)\K(?:(?!\R{2}).)*?(?-s:\bfifa\b.*\b20\b).*?(?=\R{2}|\z) to do the selection I've mentioned.) So what to do with { $_ -Split '(\r?\n)(?=(\r?\n)+)' } ?
And I'll appreciate if also help me for another command. I need to count each string (hit) which contains fifa and put it to another var and use it in cmd. I'm using this command now: for /f %%N in ('findstr /i /r /c:"FIFA.*20" ^< %LogFILE%') do set /a count+=1 if !count! lss 10 set "count=0%count%" if !count! lss 10 set "count=%count:~-2% if !count! gtr 0 echo !count! X Hit With FIFA>> %LogNEW% set count=0
Please create a new question for this, and found why you want use the rusty CMD prompt for this. PowerShell has much better semantics and is less limited.
Why is because I have a semi-big app written in cmd, I should change every single line if I wanna translate it to PS, which I don't know much about it's scripting. Thanks for your solutions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.