Issue
We use Hashicorp vault to store certs and keys. I want to retrieve the certs/keys stored in vault. With vault API command, I get below output
vault get /mysecret.key
output:
====== Data ======
Key Value
--- -----
password -----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
-----END RSA PRIVATE KEY-----
Expected output:
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
-----END RSA PRIVATE KEY-----
Tried:
vault get /mysecret.key | sed -n '/^password/,$p'
password -----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
-----END RSA PRIVATE KEY-----
How do I use grep/awk/sed to get the expected output?
Solution
With your shown attempts, please try following code.
your_API_command |
awk -v RS= 'match($0,/-+BEGIN.*END RSA PRIVATE KEY-+/){print substr($0,RSTART,RLENGTH)}'
Explanation: Simple explanation would be, run your API command and send its output as an standard input to awk
command. Where using nullify RS then using match
function to match string from -
(1 or more occurrences) followed by BEGIN
till string END RSA PRIVATE KEY
followed by 1 or more occurrences of -
.
2nd solution: A little tweaked form of 1st solution here, written and tested in GNU awk
.
your_API_command | awk -v RS='-+BEGIN.*END RSA PRIVATE KEY-+' 'RT{print RT}'
Answered By - RavinderSingh13 Answer Checked By - Timothy Miller (WPSolving Admin)