[英]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.我需要在 shell 脚本中提取公钥,不知道如何使用它。 I am new to shell script.我是 shell 脚本的新手。 https://regex101.com/r/SXDEaU/1 https://regex101.com/r/SXDEaU/1
Content of the file:文件内容:
public_key=#STARTKEY#<public key base64 encoded>#ENDKEY#
Regex: /public_key=#STARTKEY#(.*)#ENDKEY#/s
正则表达式:/ /public_key=#STARTKEY#(.*)#ENDKEY#/s
(.*)#ENDKEY#/s
Since the key is base64 encoded, it is a multiline string.由于密钥是 base64 编码的,因此它是一个多行字符串。
Desired output: <public key base64 encoded>所需的 output: <公钥 base64 编码>
perl -M5.010 -0777ne'say for /public_key=#STARTKEY#(.*)#ENDKEY#/s' file
With awk
using its RS
variable and setting it to paragraph mode please try following code.使用awk
使用其RS
变量并将其设置为段落模式,请尝试以下代码。
awk -v RS= 'match($0,/public_key=#STARTKEY#.*#ENDKEY#/){print substr($0,RSTART+21,RLENGTH-29)}' Input_file
Using sed
:使用sed
:
sed -En 's/.*#STARTKEY#(.*)#ENDKEY#/\1/p' file
A little trick that uses #
as delimiter for reading the key in pure bash:使用#
作为分隔符读取纯bash中的密钥的小技巧:
IFS='#' read -d '' _ _ key _ < key.txt
printf '%s\n' '${key//$'\n'/}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.