ERR
ERR

Reputation: 415

Grep the entire text after a certain word using grep/awk/sed

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

Answers (4)

anubhava
anubhava

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

potong
potong

Reputation: 58351

This might work for you (GNU sed):

sed -z 's/.*\npassword\s*//' file

Upvotes: 0

Ed Morton
Ed Morton

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

RavinderSingh13
RavinderSingh13

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

Related Questions