batch file parsing and returning the result
-
12-12-2019 - |
Question
I have to use a batch file and have to parse one of the parameters. The result should go to one of the commands in the batch file.
So one of the parameter for the batch file is like this:
Lorem [ABC-1234] ipsum
One of the commands in the batch file needs the ABC-1234 value from the parameter above. A regexp usage would be very nice, the FINDSTR finds it, but it returns it's input if the pattern found.
echo Lorem [ABC-1234] ipsum | findstr /R "\[[A-Z]*\-[0-9]*\]"
This returns:
echo Lorem [ABC-1234] ipsum
But I'd like to get the
ABC-1234
Solution
Assuming there are no other [
or ]
characters prior to [ABC-1234]
, then you can use FOR /F
for /f "eol=[ tokens=2 delims=[]" %%A in ("Lorem [ABC-1234] ipsum") do echo %%A
OTHER TIPS
findstr does return the whole line not only the string you would have to trim the result, for the search itself
[A-Z]*-[0-9]*
would be enough, or better
([A-Z]*-[0-9]*)
this works already as above:
echo Lorem [ABC-1234] ipsum | findstr /R [A-Z]*-[0-9]*