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---
morez890
  • 57
  • 6
  • 3
    Well, [`findstr`](https://ss64.com/nt/findstr.html) just supports a tiny subset of regular expressions, no grouping (`(`/`)`/`?`/`:`), no quantifiers (`+`/`{`/`}`/`?`) other than `*`, no alteration (`|`), etc.… – aschipfl Nov 15 '20 at 04:04
  • 4
    why are you using `findstr.exe` - a command line utility - instead of `Select-String` - a PoSh cmdlet? – Lee_Dailey Nov 15 '20 at 04:06
  • @aschipfl so it means no solution for this case? – morez890 Nov 15 '20 at 04:06
  • @Lee_Dailey can't understand your question, but I'm using CMD, not powershell. – morez890 Nov 15 '20 at 04:07
  • 1
    Leverage from PowerShell as you anyway already tried; I can't help you with the syntax though… – aschipfl Nov 15 '20 at 04:09
  • Try using GNUsed or another third-party utility (like (g)awk) often used to supplement Uncle Bill's Broken Toolbox – Magoo Nov 15 '20 at 06:06
  • 3
    I think we dealing with an [XY Problem](https://meta.stackexchange.com/a/66378/598067) here. Why are you so eager to use the old fashion and limited `CMD` with `findstr`? – iRon Nov 15 '20 at 07:52
  • @morez890 - if you are using CMD - and not PoSh - then why have you tagged this Question with `powershell`? [*grin*] – Lee_Dailey Nov 15 '20 at 15:41
  • @Lee_Dailey Because I know we can use powershell commands in cmd, and also mentioned in the question. – morez890 Nov 15 '20 at 15:48
  • @morez890 - you cannot use powershell commands in `cmd.exe` ... you can only call `powershell.exe` from inside `cmd.exe`. look at what happens when calling `G-CI` while in `cmd.exe` ... `'get-childitem' is not recognized as an internal or external command, operable program or batch file.` – Lee_Dailey Nov 15 '20 at 17:56
  • @Lee_Dailey True, but check the answer, it fixed my problem now and that was my mean actually, not using powershell command directly in cmd. – morez890 Nov 15 '20 at 19:06
  • 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*] – Lee_Dailey Nov 15 '20 at 19:26

1 Answers1

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"
iRon
  • 20,463
  • 10
  • 53
  • 79
  • 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](https://regex101.com/r/S8Kg6x/4) (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)+)' }` ? – morez890 Nov 15 '20 at 13:10
  • 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` – morez890 Nov 15 '20 at 13:54
  • 1
    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. – iRon Nov 15 '20 at 14:02
  • 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 – morez890 Nov 15 '20 at 14:11