0

I have a comma separated string that could contain quoted elements with commas. For example:

issuer=C = US, O = "DigiCert, Inc.", CN = DigiCert High Assurance TLS Hybrid ECC SHA256 2020 CA1

I would like to extract the different elements ignoring the quoted comma (DigiCert, Inc.).

The script should be POSIX compliant and run on non GNU systems.

Kusalananda
  • 333,661
Matteo
  • 9,796
  • 4
  • 51
  • 66

1 Answers1

1

Given that you don't want a general solution, i.e. you're looking for a hack and don't desire a robust solution, this seems pretty hack-ish and yet produces the right output, at least if the sample input you give is the most complex case you can reasonably encounter:

#!/usr/bin/env bash

set -o posix

grep '^[[:blank:]]Issuer:' | sed -Ee 's/^. O[[:blank:]]=[[:blank:]]("[^"]"|[^",]),.*/\1/'

Even as a hack, I'm certain this could be improved, if one had the need.

The above is nearly POSIX-compliant, and runs on my non-GNU system.

$ grep -w Issuer: /usr/local/etc/ssl/cert.pem | head -5; \
    echo '...'; grep -w Issuer: /usr/local/etc/ssl/cert.pem | tail -5
        Issuer: C = ES, O = FNMT-RCM, OU = AC RAIZ FNMT-RCM
        Issuer: C = ES, O = FNMT-RCM, OU = Ceres, organizationIdentifier = VATES-Q2826004J, CN = AC RAIZ FNMT-RCM SERVIDORES SEGUROS
        Issuer: CN = ACCVRAIZ1, OU = PKIACCV, O = ACCV, C = ES
        Issuer: C = IT, L = Milan, O = Actalis S.p.A./03358520967, CN = Actalis Authentication Root CA
        Issuer: C = US, O = AffirmTrust, CN = AffirmTrust Commercial
...
        Issuer: C = US, ST = New Jersey, L = Jersey City, O = The USERTRUST Network, CN = USERTrust ECC Certification Authority
        Issuer: C = US, ST = New Jersey, L = Jersey City, O = The USERTRUST Network, CN = USERTrust RSA Certification Authority
        Issuer: C = US, O = "VeriSign, Inc.", OU = VeriSign Trust Network, OU = "(c) 1999 VeriSign, Inc. - For authorized use only", CN = VeriSign Class 1 Public Primary Certification Authority - G3
        Issuer: C = US, O = "VeriSign, Inc.", OU = VeriSign Trust Network, OU = "(c) 1999 VeriSign, Inc. - For authorized use only", CN = VeriSign Class 2 Public Primary Certification Authority - G3
        Issuer: C = US, OU = www.xrampsecurity.com, O = XRamp Security Services Inc, CN = XRamp Global Certification Authority
$ ./test.sh < /usr/local/etc/ssl/cert.pem | head -5; \
    echo '...'; ./test.sh < /usr/local/etc/ssl/cert.pem | tail -5
FNMT-RCM
FNMT-RCM
ACCV
Actalis S.p.A./03358520967
AffirmTrust
...
The USERTRUST Network
The USERTRUST Network
"VeriSign, Inc."
"VeriSign, Inc."
XRamp Security Services Inc
Jim L.
  • 7,997
  • 1
  • 13
  • 27
  • Note that POSIX doesn't specify a bash utility nor the path of the env utility nor the shebang mechanism nor the -E option to sed (yet), nor head -5 nor tail -5. – Stéphane Chazelas Nov 09 '21 at 18:39
  • @StéphaneChazelas Point taken. The shebang was added only to assuage shellcheck.net. head and tail do not appear in the solution. They're used only to make explicit the edits to the posted input and output. – Jim L. Nov 09 '21 at 18:53
  • I changed the -nameopt parameters as suggested by @StéphaneChazelas but thanks anyway for the answer! – Matteo Nov 11 '21 at 11:06