I'm creating dkim private & public keys openssl genrsa -out dkim1024.key 1024 openssl rsa -in dkim1024.key -out dkim1024.pub -pubout -outform PEM
I have a bash script using awk to extract a key file
KEY=/usr/bin/awk 'NR>1{a[++k]=$0}END{for(i=1;i<k;i++)print a[i]}' file.key
file.key contents
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY
8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS
WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O
HYu6TIqqzlqtadfafafafdas
-----END PUBLIC KEY-----
I need to remove the lines. -----BEGIN PUBLIC KEY----- -----END PUBLIC KEY----- I've done this in my awk cmd but when I echo $KEY There are spaces. I'm guessing that these are return chars but I'm not sure how to get rid of them but preserve the key.
sh-3.2# echo $KEY
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY 8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O HYu6TIqqzlqtadfafafafdas
Thanks
You get spaces instead of lines because of word splitting. You should quote your variable around double-quotes
echo "$KEY"
This could be an alternative solution too:
awk '/-END PUBLIC KEY-/ { p = 0 }; p; /-BEGIN PUBLIC KEY-/ { p = 1 }' file.key
Output:
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY
8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS
WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O
HYu6TIqqzlqtadfafafafdas
The solution would also work even if there are other lines before or after the block.
You can also do it with Bash itself:
#!/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"
Output:
--------------------
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY
8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS
WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O
HYu6TIqqzlqtadfafafafdas
--------------------
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCSWKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1OHYu6TIqqzlqtadfafafafdas
It's not clear if you WANT the input lines all concatenated into one output line or not so take your pick based on your script:
$ 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
or use @konsolebox's awk solution, changing p;
to p{printf "%s",$0}
if you want the lines concatenated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With