Search code examples
parsingbatch-filefindstr

batch file parsing and returning the result


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