You missunderstood the regular expression syntax. rsa.
greps for strings starting with rsa
, followed by 3 to 13 repetitions of any character (.
is the wildcard character in regular expressions).
To grep for the key ID, use match groups. You cannot do this using a single grep statement, and either have to use two of them or switch to another tool, for example sed or look for other ways to solve the problem. Using GNU grep, which supports the -P
parameter you already used for perl-style regular expressions, you can use lookahead and lookbehind to achieve what you're trying to do:
echo 'rsa2048/2642B5CD' | grep -o -P '(?<=rsa2048/)[[:xdigit:]]'
This is only one way to achieve the desired result, and there are lots of other possible ways. The one above is probably one of the cleanest, an alternative might be simply cutting of the ID:
echo 'rsa2048/2642B5CD' | grep -o -P 'rsa.' | cut -d/ -f2
Anyway, for scripting purpose you should go for the colon-seperated output achieved with --with-colons
, which is much better parseable. Following lists all keys, and then filters for public keys (starting at the beginning of each line using ^
), without caring for the key's validity, but filtering RSA keys (field 4, algorithm ID 1) of size 2048 bits and finally cuts out field 5, which contains the key ID.
gpg2 --with-colons --list-keys | grep '^pub:[[:alpha:]]:2048:1:' | cut -d: -f5