1

As there is no native grep on Windows, I'm struggling to get the end of the substring located at the end of a line, which is prepended by some spaces.

My search string always has the following pattern:

Some description about something: the-value-I-want-separated-by-hyphens

(there are 5 spaces separating the description and the value)

With Regex I could do: [^ ]+$ which returns: the-value-I-want-separated-by-hyphens (see here: https://regex101.com/r/zgTGWA/1)

Yet this does not work in my cmd.

Context

I got a bunch of files, that look like this:

file.txt

Description Key1:     the-value-I-want-separated-by-hyphens
Another Description Key2:     another-value
Another Description Key3:     another-value

my testing code in the cmd: type file.txt | findstr "Key1" | findstr /R /C"[^ ]+$"

Although the first find string works great for filtering the desired line, the second one does not return the desired value.

Thanks for any advice

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Kalaschnik
  • 769
  • 1
  • 7
  • 21

1 Answers1

2

You can use wildcard substring replacement to remove the lead portion of the line. The wild card replacement is non greedy so it will only remove the section to where it finds the first set of 5 spaces. I used the colon to tighten up the matching as well.

@echo off

FOR /F "delims=" %%G IN ('type file.txt ^| findstr "Key1:"') DO (
    set "line=%%G"
    setlocal enabledelayedexpansion
    echo !line:*:     =!
    FOR /F "delims=" %%H IN ("!line:*:     =!") DO endlocal&set "result=%%H"
)
echo Result=%result%
pause

And based on your input file this is the result.

C:\BatchFiles\substring>so.bat
the-value-I-want-separated-by-hyphens
Result=the-value-I-want-separated-by-hyphens
Press any key to continue . . .
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • Hey Squashman, how can I use the result as a variable outside of the loop: I was trying to set a variable right below the echo: with `set "result=!line:*: =!"`. Then I want to echo the result after the for loop with: `echo %result%`. Yet this does not work. Sorry but I am super newb in bash scripting... – Kalaschnik Apr 09 '21 at 06:31
  • Saving the echo to a file, as a workaround, does also not work... `echo !line:*: =! > .temp.txt`. Is there anything I can do @squashman? – Kalaschnik Apr 09 '21 at 08:00
  • @Kalaschnik `endlocal&set "result=!line:*: =!"` – Squashman Apr 09 '21 at 13:54
  • Oh man, bash scripting is really its own world... I’ll try that when I’m home. Thanks! – Kalaschnik Apr 09 '21 at 14:40
  • @Kalaschnik this is not `BASH` scripting. This is a Windows batch file. Read this answer: https://stackoverflow.com/questions/3262287/make-an-environment-variable-survive-endlocal/8254331#8254331 – Squashman Apr 09 '21 at 14:41
  • Sorry, this was a typo. Indeed I meant batch/cmd scripting. This syntax is just from another world... – Kalaschnik Apr 09 '21 at 14:45
  • @Kalaschnik you need the variable to survive out of the local environment created by the `SETLOCAL` command. You essentially have to turn it into a GLOBAL variable. Many programming languages have this concept. This is just how it is implemented with a batch file. – Squashman Apr 09 '21 at 15:14
  • Just tested it, but the `result` variable contains `!line:*: =!`. The echo does work. The cods is looking like this: https://pastebin.com/wWgL2jdr I know this is not in the SO scope anymore, but I would be super glad to get this variable set – Kalaschnik Apr 09 '21 at 19:48
  • 1
    @Kalaschnik, sorry that was my bad. Shame on me for not completely reading the link I provided earlier. Code is updated and tested. – Squashman Apr 09 '21 at 20:02
  • <3 That's it. Thank you so much. – Kalaschnik Apr 09 '21 at 20:08
  • I know you are not my personal army, but maybe you know whats going on: The snippet is part of a nested for loop (I am looping over all files in a folder). However, once it is nested the `return` echo prints: `ECHO is off.` Context: https://pastebin.com/8571SNNd – Kalaschnik Apr 10 '21 at 08:40