Search code examples
bashshellgnupg

Extract gpg fingerprint


I'm trying to extract the fingerprint from a key in my keyring and I can't figure out how to parse the output.

Running

gpg --fingerprint 'Identifier'

Outputs

pub   rsa3072 2021-12-14 [SC]
      ABCD EFGH 1234 5678 ABCD  ABCD EFGH 1234 5678 ABCD
uid           [ unknown] First Last (Identifier) <First.Last@email.com>
sub   rsa3072 2021-12-14 [E]

I want to extract the short or long fingerprint ABCD EFGH 1234 5678 ABCD ABCD EFGH 1234 5678 ABCD

Adding a --with-colons to the call ends up printing multiple fingerprints

...
fpr:::::::::ABCDEFGH12345678ABCDABCDEFGH12345678ABCD:
...
fpr:::::::::1234123412341234123412341234123412341234:

What is the best method for extracting the fingerprint for a public and secret key in my key ring?

gpg --version
gpg (GnuPG) 2.2.27

Solution

  • Using sed

    $ gpg --fingerprint | sed -n '/^\s/s/\s*//p'
    ABCD EFGH 1234 5678 ABCD  ABCD EFGH 1234 5678 ABCD
    

    -n - Silence the output

    /^\s/ - Match lines that begin with a space

    s/\s*//p - Remove the leading spaces. Print.