Skip to content
Advertisement

Extract part of string in Bash

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

JavaScript

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

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

JavaScript

Advertisement

Answer

Try to use grep:

JavaScript

-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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement