I am writing a little script to manage keystores and need some help with sed.
The output of the keystore file is like:
vi 2, Dec 7, 2012, trustedCertEntry,
Certificate fingerprint (MD5): F9:1F:FE:E6:A3:CB:99:88:44:D4:67:ED:G5:F8:97:7A
system@remote-server, Dec 17, 2013, PrivateKeyEntry,
Certificate fingerprint (MD5): 2A:DD:4B:C5:AD:B0:MF:96:51:B0:14:4A:B5:6C:99:FB
verisign intermediate 1, Dec 7, 2012, trustedCertEntry,
Certificate fingerprint (MD5): 4E:F6:A0:FC:40:0C:AE:9C:05:2F:AE:99:C6:7D:37:9F
So what I want, is the hash sum of "system@remote-server" (e.g. 2A:DD:4B:C5:AD:B0:MF:96:51:B0:14:4A:B5:6C:99:FB )
What I tried as far is:
sed -e "system@remote-server.*PrivateKeyEntry,/,/Certificate fingerprint.*:[a-zA-Z0-9]{2}$/ {/\([[a-zA-Z0-9]{2}[:]]*[a-zA-Z0-9]{2}$\)/\1/p}"
and
sed -n "s/system@remote-server.*PrivateKeyEntry.*Certificate fingerprint:*([a-zA-Z0-9_-]): \([[a-zA-Z0-9]{2}[:]]*[a-zA-Z0-9]{2}$\)/\1/p"
but I don't get any output for them.
ps: the easy way would be grep -A 1 "system@remote-server.*PrivateKeyEntry" | tail -1 | cut -f 4 -d " " , but it is not reliable. Therefore I want to do it with sed.
awk '
f{ # if f=1 then (if a match was found previously)
print $NF # print the last field
f=0 # reset f ( set found is false)
}
/system@remote-server/{ # if a match is found
f=1 # then set f to 1
}
' file
sed '
/system@remote-server/!d # if a match is found then do not delete the line
N # append the next line to the current line
s/.* // # delete everything unto and including the last space
' file
In your hash, I doubt you have :MF:
In general, it's a serie of hex digit.
Your pattern 2 is not pattern 1 0-infinite times, you can simplified your pattern 2 as: ([A-Z0-1:]*)
If you want pattern 1 0-infinite, you can do as: (([A-Z0-1][A-Z0-1]:)*)
And so, pattern 3: (([A-Z0-1][A-Z0-1]:)*[A-Z0-1][A-Z0-1])
And if you want that your pattern is a regex for your hash, the pattern 3 will: (([0-9A-F][0-9A-F]:)*[0-9A-F][0-9A-F])
If you want the regex to match the fingerprint, try:
sed -rn 's/.*(([A-F0-9]{2}:){15}[A-F0-9]{2})/\1/p' file
This does not necessarily work with all sed versions, and, as pointed out before, it matches HEX numbers only - if you really have sth. like MF in there, adapt the ranges.