Home > Software design >  Extract gpg fingerprint
Extract gpg fingerprint

Time:12-15

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) <[email protected]>
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

CodePudding user response:

Try:

gpg --fingerprint | grep -vE "pub|uid|sub|-----|$^" | awk '{$1=$1;print}'
  • grep -v removes lines that match
  • -E is for using patterns
  • each pattern is separated by a |, meaning or
  • pattern $^ removes empty lines
  • the awk command removes leading and trailing spaces

Ex. on my linux instance:

$ gpg --fingerprint | grep -vE "pub|uid|sub|-----|$^"|awk '{$1=$1;print}'
ABCD EFGH 1234 5678 ABCD ABCD EFGH 1234 5678 ABCD

CodePudding user response:

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.

  • Related