You missunderstood the regular expression syntax. rsa.{3,13}
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:]]{8}'
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.{3,13}' | 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
thanks, great ideas. FYI --with-colons gives 64bit key ids. is my usage of sort | uniq -d ok? – StackAbstraction – 2015-08-31T22:27:35.117
What do you want to achieve using
sort | uniq -d
? I think you missed posting some important part of your question, like "I want to find duplicate short key IDs". I removed some part regarding "mass generation", which (still) seems irrelevant without further explanation and for your question as-is. – Jens Erat – 2015-08-31T22:34:45.017Correct I want to find duplicate short key IDs. so I take the grep list of short key IDs, and then sort then and test for duplicates with uniq -d. However it does not work since it provides false duplicates. – StackAbstraction – 2015-08-31T23:40:53.393