gpt4 book ai didi

regex - grep 匹配整个证书?

转载 作者:行者123 更新时间:2023-12-04 18:26:33 26 4
gpt4 key购买 nike

我正在运行生成这样的证书请求的命令,

some text
some text
certificate is -----BEGIN CERTIFICATE REQUEST-----
MIIEiDCCAnACAQAwGDEWMBQGA1UEAxMNdGVzdHZhdWx0LmNvbTCCAiIwDQYJKoZI
hvcNAQEBBQADggIPADCCAgoCggIBALoGVu7JqQ/60rEZhP2AvRRxf0p9PcQLB8KU
yy/39m2FX5gwPbjzdmenCYzTRV/CHFh3FnK+x+UwCBkAHIBRJfyl5V/V2sdqh0Tm
YEAPQVIoWcH/P7Q8FSxdnBXepnZak33PHt6We76TdxlmJ+OSLPT4XzKX7gTPGSxW
L0tEuzLxkMieigx8TFtLGpe5CWshOe/oiUHR6g9Ehf6s4vIvT7quSGYJWgfD7PI0
+QfuOhg2ldlNKnDm+chkHFYO2rVS+42zhuGk8C/wIElgrsICl1Xpzna2ZmkuscF9
Gir2HVGPtkxB13CyK9jne8Pxr6jRwkGPny7x4czZfafz3sgVo2WmonmaXpXp8od4
aBs1b1VhJuegWo6j2r8TS9scG5o4GTxwCXNeFQnXB+tWDRV7MH6DW/gWEBV/e0QB
RKfhlPhEUd8bgzYwe8gi6ikIoY7RpyXMeV5iVwU3498+ok0v1JlF813Fdx/47nLe
AY/w3vrTJdioJ6y7h1H9GQGb1vpx9HaXsJEpSVxeeNygAJZpuWXOFBiGF2EpN0Uq
YllwAGHYe9ihKXCrRt8rgust+iE4uEw+ypRxNMTGiPxv+jkEwmd5SZODXoSh6OVb
y06bGIilBbITO5bG0bXJrOFOZzOP37oEwX/8+zNX8lEhPh4IlizwA83Rl08wkk2N
H8lL+hnBAgMBAAGgKzApBgkqhkiG9w0BCQ4xHDAaMBgGA1UdEQQRMA+CDXRlc3R2
YXVsdC5jb20wDQYJKoZIhvcNAQELBQADggIBAIwP3a80GQJJoyqO3CPPp8Kbpqtd
a11nmSLlw7U+0YRIElk1Z0oTaE1AURnEGcm2URFTMZYqHXDcMYhP4lcNp3kHSj/C
8UOT75rRDjWfkEpHdGdUOKUuLcJxjzWw0BiSsl7Bnz1aG+7qJ/iv2hYOmyDiR4g/
Tt+IIeShJpMxM63y47BzeLmQgugqyLKvgGsyeeQYCblXUBMla4geNhgBnZikGiIy
8MTvhh24CuBPi9DdGwDO8Wly4BKseBTSAAQrsbrkmSbTunSb/6ry83UDV20f2inv
+Y7UaYi6Ww07fX02oMoRY7d1M9hRDrpD5ZRP3huCDHIvLlY+tJdZL0YTHZM/wk+Z
kg44xXOEGNePoDI8RhOSOe6ixSRFt9stW+Mpe19R0UF4tA6YmNMyt43ILcljEHAh
b20SVTCbN9SICccdTjh3qxIXYK2HlRe99Tqo6NTt1xdE3AuJAjZZXDtfKqmqXMi+
HDeeQpYot3spjlklqEVZYqsWOvvWoGKnAjOLBHxD10IaqF4Epnt/OZKq01ZVIRp2
+I09tsiIpHYJCC4br0k6xwz8SUbpPUGiJ+j8N0rcd8I7xjToXtauQ9a9pAHmf0g8
8w5ZqhjSj6//xbFIhrK+SyO/R/W2s2XeUfcBmlyJFb/2zOKZ6CzbCgsjZ8xyfk13
ltjjnB1ecB0sWgpu
-----END CERTIFICATE REQUEST-----

我想将证书自动保存到一个文件中,而不是文本 certificate is 或之前的任何行。

现在我正在使用命令 | grep -A100 "\-----BEGIN" 获取所有行并将其保存到文件但我只是使用 100 作为行数,但证书不是 100 行。如何获得包括 -----BEGIN 在内的确切行数,以便将证书写入文件?

编辑:我也试过这个,command | grep -A$(wc -l) "\-----BEGIN" 但它根本不打印任何内容

谢谢。

最佳答案

使用 GNU awk 进行多字符 RS 和 RT:

$ awk -v RS='-----BEGIN CERTIFICATE REQUEST-----.*-----END CERTIFICATE REQUEST-----' 'RT{print RT}' file
-----BEGIN CERTIFICATE REQUEST-----
MIIEiDCCAnACAQAwGDEWMBQGA1UEAxMNdGVzdHZhdWx0LmNvbTCCAiIwDQYJKoZI
hvcNAQEBBQADggIPADCCAgoCggIBALoGVu7J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i6Ww07fX02oMoRY7d1M9hRDrpD5ZRP3huCDHIvLlY+tJdZL0YTHZM/wk+Z
kg44xXOEGNePoDI8RhOSOe6ixSRFt9stW+Mpe19R0UF4tA6YmNMyt43ILcljEHAh
b20SVTCbN9SICccdTjh3qxIXYK2HlRe99Tqo6NTt1xdE3AuJAjZZXDtfKqmqXMi+
HDeeQpYot3spjlklqEVZYqsWOvvWoGKnAjOLBHxD10IaqF4Epnt/OZKq01ZVIRp2
+I09tsiIpHYJCC4br0k6xwz8SUbpPUGiJ+j8N0rcd8I7xjToXtauQ9a9pAHmf0g8
8w5ZqhjSj6//xbFIhrK+SyO/R/W2s2XeUfcBmlyJFb/2zOKZ6CzbCgsjZ8xyfk13
ltjjnB1ecB0sWgpu
-----END CERTIFICATE REQUEST-----

使用任何 awk:

$ awk -v beg='-----BEGIN CERTIFICATE REQUEST-----' -v end='-----END CERTIFICATE REQUEST-----' 'sub(".*"beg,beg){f=1} f; sub(end".*",end){exit}' file
-----BEGIN CERTIFICATE REQUEST-----
MIIEiDCCAnACAQAwGDEWMBQGA1UEAxMNdGVzdHZhdWx0LmNvbTCCAiIwDQYJKoZI
hvcNAQEBBQADggIPADCCAgoCggIBALoGVu7JqQ/60rEZhP2AvRRxf0p9PcQLB8KU
yy/39m2FX5gwPbjzdmenCYzTRV/CHFh3FnK+x+UwCBkAHIBRJfyl5V/V2sdqh0Tm
YEAPQVIoWcH/P7Q8FSxdnBXepnZak33PHt6We76TdxlmJ+OSLPT4XzKX7gTPGSxW
L0tEuzLxkMieigx8TFtLGpe5CWshOe/oiUHR6g9Ehf6s4vIvT7quSGYJWgfD7PI0
+QfuOhg2ldlNKnDm+chkHFYO2rVS+42zhuGk8C/wIElgrsICl1Xpzna2ZmkuscF9
Gir2HVGPtkxB13CyK9jne8Pxr6jRwkGPny7x4czZfafz3sgVo2WmonmaXpXp8od4
aBs1b1VhJuegWo6j2r8TS9scG5o4GTxwCXNeFQnXB+tWDRV7MH6DW/gWEBV/e0QB
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QUEST-----

关于regex - grep 匹配整个证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51690740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com