Reputation: 415
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?
Upvotes: 2
Views: 394
Reputation: 784908
You may use this sed
:
vault get /mysecret.key | sed -n '/^password[[:blank:]]*/,$ {s///; p;}'
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
-----END RSA PRIVATE KEY-----
Command Details:
-n
: Disable normal printing/^password[[:blank:]]*/,$
: Pattern match start from password
followed by 0 or whitespaces till end of file{s///; p;}
: For matching block do a substitute by removing password
followed by 0+ whitespace from output and using p
just print it.Upvotes: 2
Reputation: 58351
This might work for you (GNU sed):
sed -z 's/.*\npassword\s*//' file
Upvotes: 0
Reputation: 203169
$ vault get /mysecret.key | awk 'sub(/^password[[:space:]]*/,""){f=1} f'
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
-----END RSA PRIVATE KEY-----
Upvotes: 1
Reputation: 133428
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}'
Upvotes: 2