Search code examples
windowsloopsfor-loopcommandcut

Unix and Linux Cut command to equivalent with the For Loop DOS command


Scenario

I have this OpenSSL command with an output to a pipe for a cut.

openssl x509 -serial -noout -in cacert.pem | cut -d= -f2 > serial

I would like to keep only the serial number but with loop for cmd

openssl x509 -serial -noout -in cacert.pem
serial=E314E555E2D52FC8

Example of the result I hope

openssl x509 -serial -noout -in cacert.pem | FOR /F "tokens=2 delims==" %G IN ("pipe=result") DO @echo %G>serial

Solution

  • DOS doesn't have a FOR loop like the one you're trying, but I suspect you don't mean DOS as OpenSSL has not been ported to that operating system. If you mean Windows, then the command you need is:

    (for /f "tokens=2 delims==" %a in ('openssl x509 -serial -noout -in cacert.pem') do @echo %a) > serial
    

    If you put the above in a .bat file, you'll need to change %a to %%a.