sh GetPrivateKey appname
返回低于输出
-----BEGIN RSA PRIVATE KEY----- JDkldlkdlkdldldlk1G/XhgJk4TXeQ06OIunFDUVfWcvCZ+X-dkjdkjdjkdjkdjk B4SFZdjSo+fU2122jkslOreX7QYl8P3tL3BYg6dkHGkj9tbi83XrpbbRu2f0QOX3 PoiCOsV0BOiqXEUGLEQ6pWwNv9ZJCGvT/UFfmIl1SdlIMdkFDBGNhVyGz74ZS/ph OsDAwRr6yVA0V63L7ze0C31E1S1axXmD5zFnWltgNNGgTocVoEy0DeW46YNc/hms R2LeH3qpbnoMs/ZgxEYITPh3V+NQkLB5buVvQflNnYm4bOrWUy98t+hIV3mnM+wV CqW5L9dvI8qYx/fU65PdEJmIadAvP8pj4UtrwwIDAQABAoIBADQ53ubdX/iCe1Ji j4IMGbXnzLDagIiLVd7ekzyAWaJu5YjZhfoSqXlbVevGTvlpB907dPpwB28/9r6R sz3nMSkQhLB017uYrixnd17USQ9dCMPFUg61ZCIsTJkt+3SkZnsoPapGgztnNVOM jsdklsdklsdklsdklsdklsdklsdklsdFUg61ZCIsTJkt+3SkZnsoPapGgztnNVOM NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR ywU7RqGelopd736SE4C2eFqLAhpiC+oxl7FmEIm1llITK5tlW8CK1F7MStK/akA/ JDsk1mKlsKCtDEU5c7TY2JDaTLA1qnoLFUPeiHMuCwoWBSVK9Irp3xsCgYEA4gca M3zbki6ToJ/hDNoNC5RrZI4o3KJDG9x97d8hYk0h3hhJCjFXr9MSm8Xwtp5SnAxS /bNHPSL1Wi+ZOIP4nNUG5Kto0KovPFf3U0c+LpH5kNGest9jnr49K+i5JDIItQQY NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR BJWcRmSBKOjuqk+LRLmOEBcAn9yqCZ6tXIMUyj/LEi4ArpHHr47h9bUs5AMoD/Zo cDFIozreSs41SwTh9VO9vHvnOzbTCD3uRKrsTikjyz7D+0gZtK/wa4dcEFIhHBKC NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR ZC7XIQ+yNaz6rkewVNP1pGfbRDWhtYDaRhoNws10leOyoqPL5Iea1fHjTSxQYROn NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR /RxlD3ECgYAFthRafjyETgE4o8RmAraYEqK9eYLnDG/pB9zsaq+Mlh8BExU7OWqx NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR VP9s6+olQVf7ZJyVlr9HydMBd/xdKhzXFH15AfTgIhKKzHe/OcBJTA== -----END RSA PRIVATE KEY-----
我想将其格式化为私钥。所以我写了下面的命令,它在上面的输出中找到空格("(并逐行显示,但我不想拆分-----BEGIN RSA PRIVATE KEY-----
和-----END RSA PRIVATE KEY-----
(包括它们末尾的空格(
[user@server]$ echo `sh GetPrivateKey appname` | tr " " "n"
-----BEGIN
RSA
PRIVATE
KEY-----
JDkldlkdlkdldldlk1G/XhgJk4TXeQ06OIunFDUVfWcvCZ+X-dkjdkjdjkdjkdjk
B4SFZdjSo+fU2122jkslOreX7QYl8P3tL3BYg6dkHGkj9tbi83XrpbbRu2f0QOX3
PoiCOsV0BOiqXEUGLEQ6pWwNv9ZJCGvT/UFfmIl1SdlIMdkFDBGNhVyGz74ZS/ph
OsDAwRr6yVA0V63L7ze0C31E1S1axXmD5zFnWltgNNGgTocVoEy0DeW46YNc/hms
R2LeH3qpbnoMs/ZgxEYITPh3V+NQkLB5buVvQflNnYm4bOrWUy98t+hIV3mnM+wV
CqW5L9dvI8qYx/fU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/RxlD3ECgYAFthRafjyETgE4o8RmAraYEqK9eYLnDG/pB9zsaq+Mlh8BExU7OWqx
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
VP9s6+olQVf7ZJyVlr9HydMBd/xdKhzXFH15AfTgIhKKzHe/OcBJTA==
-----END
RSA
PRIVATE
KEY-----
我认为sed
命令满足了我的需求,但我对Linux命令还很陌生。我们将感谢您的帮助。
预期输出:
-----BEGIN RSA PRIVATE KEY-----
JDkldlkdlkdldldlk1G/XhgJk4TXeQ06OIunFDUVfWcvCZ+X-dkjdkjdjkdjkdjk
B4SFZdjSo+fU2122jkslOreX7QYl8P3tL3BYg6dkHGkj9tbi83XrpbbRu2f0QOX3
PoiCOsV0BOiqXEUGLEQ6pWwNv9ZJCGvT/UFfmIl1SdlIMdkFDBGNhVyGz74ZS/ph
OsDAwRr6yVA0V63L7ze0C31E1S1axXmD5zFnWltgNNGgTocVoEy0DeW46YNc/hms
R2LeH3qpbnoMs/ZgxEYITPh3V+NQkLB5buVvQflNnYm4bOrWUy98t+hIV3mnM+wV
CqW5L9dvI8qYx/fU65PdEJmIadAvP8pj4UtrwwIDAQABAoIBADQ53ubdX/iCe1Ji
j4IMGbXnzLDagIiLVd7ekzyAWaJu5YjZhfoSqXlbVevGTvlpB907dPpwB28/9r6R
sz3nMSkQhLB017uYrixnd17USQ9dCMPFUg61ZCIsTJkt+3SkZnsoPapGgztnNVOM
jsdklsdklsdklsdklsdklsdklsdklsdFUg61ZCIsTJkt+3SkZnsoPapGgztnNVOM
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
ywU7RqGelopd736SE4C2eFqLAhpiC+oxl7FmEIm1llITK5tlW8CK1F7MStK/akA/
JDsk1mKlsKCtDEU5c7TY2JDaTLA1qnoLFUPeiHMuCwoWBSVK9Irp3xsCgYEA4gca
M3zbki6ToJ/hDNoNC5RrZI4o3KJDG9x97d8hYk0h3hhJCjFXr9MSm8Xwtp5SnAxS
/bNHPSL1Wi+ZOIP4nNUG5Kto0KovPFf3U0c+LpH5kNGest9jnr49K+i5JDIItQQY
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
BJWcRmSBKOjuqk+LRLmOEBcAn9yqCZ6tXIMUyj/LEi4ArpHHr47h9bUs5AMoD/Zo
cDFIozreSs41SwTh9VO9vHvnOzbTCD3uRKrsTikjyz7D+0gZtK/wa4dcEFIhHBKC
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
ZC7XIQ+yNaz6rkewVNP1pGfbRDWhtYDaRhoNws10leOyoqPL5Iea1fHjTSxQYROn
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
/RxlD3ECgYAFthRafjyETgE4o8RmAraYEqK9eYLnDG/pB9zsaq+Mlh8BExU7OWqx
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
VP9s6+olQVf7ZJyVlr9HydMBd/xdKhzXFH15AfTgIhKKzHe/OcBJTA==
-----END RSA PRIVATE KEY-----
您可以尝试此gnu awk
:
awk -v RS='-{5}(BEGIN|END) [^-]* KEY-{5}' '{
gsub(/ /, "n"); ORS=RT} 1' file.rsa
或使用perl
:
perl -pe 's/-{5}(BEGIN|END) [^-]* KEY-{5}(*SKIP)(*F)|h/n/g' file.rsa
输出:
-----BEGIN RSA PRIVATE KEY-----
JDkldlkdlkdldldlk1G/XhgJk4TXeQ06OIunFDUVfWcvCZ+X-dkjdkjdjkdjkdjk
B4SFZ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bUs5AMoD/Zo
cDFIozreSs41SwTh9VO9vHvnOzbTCD3uRKrsTikjyz7D+0gZtK/wa4dcEFIhHBKC
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
ZC7XIQ+yNaz6rkewVNP1pGfbRDWhtYDaRhoNws10leOyoqPL5Iea1fHjTSxQYROn
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
/RxlD3ECgYAFthRafjyETgE4o8RmAraYEqK9eYLnDG/pB9zsaq+Mlh8BExU7OWqx
NuFxSBXKOecYV2I5sfo9owjOEQFt2O/1D2KCvMETgF9Fqi99321Fqe9aKHLM2NCR
VP9s6+olQVf7ZJyVlr9HydMBd/xdKhzXFH15AfTgIhKKzHe/OcBJTA==
-----END RSA PRIVATE KEY-----
根据您显示的示例,您可以尝试以下用GNUawk
编写的内容吗。
awk '
match($0,/KEY-+.*-+END/){
val=substr($0,RSTART,RLENGTH)
num=split(val,arr," ")
print substr($0,1,RSTART-1) arr[1]
for(i=2;i<num;i++){
print arr[i]
}
print arr[num] substr($0,RSTART+RLENGTH)
}
' Input_file
解释:添加以上详细解释。
awk ' ##Starting awk program from here.
match($0,/KEY-+.*-+END/){ ##Using match function to match regex KEY-+.*-+END in current line.
val=substr($0,RSTART,RLENGTH) ##Creating val which has sub string of matched regex.
num=split(val,arr," ") ##Splitting val into array arr.
print substr($0,1,RSTART-1) arr[1] ##printing sub string before matched regex character and printing array 1st element.
for(i=2;i<num;i++){ ##Going through all elements of arr here.
print arr[i] ##Printing arr element with index i here.
}
print arr[num] substr($0,RSTART+RLENGTH) ##Printing rest of line after matched regex.
}
' Input_file
使用任何awk:
$ awk -v OFS='n' '{
beg = end = $0
sub(/- .*/,"-",beg)
sub(/.* -/,"-",end)
gsub(/^.*- | -.*$/,"")
$1 = $1
print beg, $0, end
}' file
-----BEGIN RSA PRIVATE KEY-----
JDkldlkdlkdldldl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