nick
nick

Reputation: 17

How to use this regex with grep in shell script?

I need to extract the public key in the shell script, Not sure how to use this. I am new to shell script. https://regex101.com/r/SXDEaU/1

Content of the file:

public_key=#STARTKEY#<public key base64 encoded>#ENDKEY#

Regex: /public_key=#STARTKEY#(.*)#ENDKEY#/s

Since the key is base64 encoded, it is a multiline string.

Desired output: <public key base64 encoded>

Upvotes: 0

Views: 139

Answers (2)

ikegami
ikegami

Reputation: 385590

perl -M5.010 -0777ne'say for /public_key=#STARTKEY#(.*?)#ENDKEY#/s' file

The key is -0777 which causes the entire file to be read in as one line.

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133458

With awk using its RS variable and setting it to paragraph mode please try following code.

awk -v RS= 'match($0,/public_key=#STARTKEY#.*#ENDKEY#/){print substr($0,RSTART+21,RLENGTH-29)}'  Input_file

Upvotes: 2

Related Questions