0

When I import my GPG keys, I get a response back:

gpg: key LOL12345: public key "John Doe (Developer) <john@doe.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

I would very much like to extract the key's ID LOL12345.

The command I run that returns the output is as follows:

gpg --import "public.key"
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
rugaard
  • 81
  • 1
  • 9
  • probably the `gpg` command has some option to list only keys... skimmed through man page and saw options like `--list-keys`, `-k` etc.. see if that helps – Sundeep Oct 24 '16 at 16:15

2 Answers2

2

Try to use grep:

yourCommand | grep -Po -m 1 'gpg: key \K\w+'

-P use perl regex style.
-o print only the matched part.
-m 1 exit after the first match. This will ensure, that the key is not printed multiple times.
\K once matched, forget the matched part left of \K.
\w+ Match as many alphanumeric characters as possible.

Socowi
  • 25,550
  • 3
  • 32
  • 54
  • When executing the `gpg` command it's like the output is never "caught" by `|` and the hereafter method. It just outputs exactly the same as before as if it's ignoring everything after the `|`. – rugaard Oct 24 '16 at 14:21
  • 2
    Does gpg print to stderr? If so, redirect the output. See [this answer](http://stackoverflow.com/a/2342841/6770384). – Socowi Oct 24 '16 at 14:23
  • It did! Your link helped me out. But I liked @heemayl method with `awk` a bit better, so my answer was a mix of both your answers :D – rugaard Oct 24 '16 at 14:25
1

With awk:

gpg --import "public.key" | awk -F '[ :]' 'NR==1 {print $4; exit}'
  • -F '[ :]' sets the field delimiter as space or :

  • NR==1 matches the first line, {print $4; exit} prints the 4th field (desired field), and then exits

If gpg is output-ing to STDERR:

gpg --import "public.key" |& awk -F '[ :]' 'NR==1 {print $4; exit}'

For an older bash:

gpg --import "public.key" 2>&1 | awk -F '[ :]' 'NR==1 {print $4; exit}'

Example:

% cat file.txt
gpg: key LOL12345: public key "John Doe (Developer) <john@doe.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
gpg: key LOL12345: secret key imported
gpg: key LOL12345: "John Doe (Developer) <john@doe.com>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
gpg:       secret keys read: 1
gpg:   secret keys imported: 1

% awk -F '[ :]' 'NR==1 {print $4; exit}' file.txt
LOL12345
heemayl
  • 39,294
  • 7
  • 70
  • 76
  • When executing the `gpg` command it's like the output is never "caught" by `|` and the hereafter method. It just outputs exactly the same as before as if it's ignoring everything after the `|`. – rugaard Oct 24 '16 at 14:20
  • 1
    @MortenRugaard Probably because `gpg` is sending the output to STDERR, do: `gpg --import "public.key" |& awk -F '[ :]' 'NR==1 {print $4; exit}'` – heemayl Oct 24 '16 at 14:21
  • Oh okay, so in you're example you're using `awk` with a file. But how do I write a temp. file with the output if it's ignoring everything after `|`? – rugaard Oct 24 '16 at 14:22
  • Now I'm getting `-bash: syntax error near unexpected token `&'` – rugaard Oct 24 '16 at 14:24
  • 1
    @MortenRugaard Check my edits. Do: `gpg --import "public.key" 2>&1 | awk -F '[ :]' 'NR==1 {print $4; exit}'` – heemayl Oct 24 '16 at 14:25
  • @Socowi Had the answer to grab the output: http://stackoverflow.com/questions/40220672/extract-part-of-string-in-bash#comment67705024_40220763 But I liked your way with using `awk` better, the answer was a mix of both your answers. – rugaard Oct 24 '16 at 14:26