BASH脚本使用awk提取密钥

时间:2014-08-14 05:17:57

标签: linux bash shell awk

我正在创建dkim private&公钥 openssl genrsa -out dkim1024.key 1024 openssl rsa -in dkim1024.key -out dkim1024.pub -pubout -outform PEM

我有一个使用awk提取密钥文件的bash脚本 KEY = /usr/bin/awk 'NR>1{a[++k]=$0}END{for(i=1;i<k;i++)print a[i]}' file.key

file.key内容

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY
8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS
WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O
HYu6TIqqzlqtadfafafafdas
-----END PUBLIC KEY-----

我需要删除这些行。 -----开始公钥----- ----- END PUBLIC KEY ----- 我在我的awk cmd中完成了这个,但当我回显$ KEY时有空格。我猜这些都是返回字符,但我不确定如何摆脱它们但保留密钥。

sh-3.2# echo $KEY
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY 8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O HYu6TIqqzlqtadfafafafdas

由于

2 个答案:

答案 0 :(得分:6)

由于word splitting,您获得的是空格而不是行。你应该用双引号引用你的变量

echo "$KEY"

这也可以是一种替代解决方案:

awk '/-END PUBLIC KEY-/ { p = 0 }; p; /-BEGIN PUBLIC KEY-/ { p = 1 }' file.key

输出:

MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY
8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS
WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O
HYu6TIqqzlqtadfafafafdas

即使在阻止之前或之后还有其他行,解决方案也会起作用。

您也可以使用Bash本身:

#!/bin/bash

FILE='file.key'  ## Or FILE=$1

KEY=()
while IFS=$' \t\r\n' read -r LINE; do
    [[ $LINE == *'-END PUBLIC KEY-'* ]] && P=0
    (( P )) && KEY+=("$LINE")  ## Store every line as an array element.
    [[ $LINE == *'-BEGIN PUBLIC KEY-'* ]] && P=1
done < "$FILE"

echo --------------------

printf '%s\n' "${KEY[@]}"

echo --------------------

IFS= eval 'MERGED_KEY="${KEY[*]}"'  ## Merge key without spaces.

echo "$MERGED_KEY"

输出:

--------------------
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY
8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS
WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O
HYu6TIqqzlqtadfafafafdas
--------------------
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCSWKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1OHYu6TIqqzlqtadfafafafdas

答案 1 :(得分:1)

目前尚不清楚你是否想要将输入行全部连接到一个输出行,所以根据你的脚本选择你的选择:

$ key=$(awk 'NR>1{a[++k]=$0}END{for(i=1;i<k;i++)print a[i]}' file)
$ echo "$key"
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY
8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS
WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O
HYu6TIqqzlqtadfafafafdas

$ key=$(awk 'NR>1{a[++k]=$0}END{for(i=1;i<k;i++)printf "%s",a[i]}' file)
$ echo "$key"
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCSWKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1OHYu6TIqqzlqtadfafafafdas

或使用@ konsolebox的awk解决方案,如果您希望连接这些行,请将p;更改为p{printf "%s",$0}