openssl x509 -noout -serial -in cert.pem | cut -d'=' -f2 | sed 's/../&:/g;s/:$//'
openssl x509 -noout -serial -in cert.pem will output the serial number of the certificate, but in the format
It is therefore piped to
cut -d'=' -f2 which splits the output on the equal sign and outputs the second part -
That is sent to
sed. The first part of the
s/../&:/g splits the string every two characters (
..) and inserts a colon (
:). This results in
01:23:45:67:89:AB: (note the colon on the end).
The second part of the
sed command (
s/:$//) searches for a colon at the end of the output and replaces it with an empty string, resulting in the desired output.
Or for a
sed only answer:
openssl x509 -noout -serial -in test2.crt | sed 's/.*=//g;s/../&:/g;s/:$//'
The addition of
s/.*=//g at the start of the
sed command replaces the
cut in the first version.