I have a config file (ssl.cfg) for SSL requests:
oid_section = OIDs
[req]
default_bits = 4096
prompt = no
default_md = sha256
distinguished_name = dn
attributes = v3_req
[OIDs]
OrganizationID = 2.5.4.97 # Don't touch this
[dn]
C = FO
ST = Foobar Monarchy
O = Lorem Ipsum
CN = specific.domain.com
OrganizationID = ABCDE-12345 # Change this
#...
[v3_req]
#...
I'm trying to replace the second OrganizationID's value (ABCDE-12345) with a new value, ABCDE-98765.
The problem is that most matches against OrganizationID conflict between the two identical names, which have to be the same (the one under [OIDs] section is marking a label; the second occurence is actually using the label).
Things that I've tried:
1) Adapted code from this answer (Replace line after match) on Stack Overflow:
REQ_ID="ABCDE-98765"
sed -i "/CN =/!b;n;c\\OrganizationID = ${REQ_ID}" ssl.cfg
- matches the previous line,
CN = ... !bbreaks the current matchnscans the next linecchanges the selected line with text that follows (\prints a new line; escaped (\\) because of double-quotes)
I don't really like this solution, because of its dependency on the position of the previous line. If someone manually swaps the CN to a different line, this will break.
2) Using the previous attempt's knowledge
REQ_ID="ABCDE-98765"
sed -i "/\\\[dn\\\]/!b;/OrganizationID/c\\OrganizationID = ${REQ_ID}" ssl.cfg
Here, I tried:
- matching
[dn](double-escaped[and]) - breaking the current match
- Looking for the next occurence of OrganizationID from that point onward
- replacing using
c\...
This doesn't work; I have a feeling the second match goes from the top all over again. I've noticed that if both occurences are matched, neither seem to change.
I'm specifically looking for a way how to match the OrgID without caring about its position within [dn].
The string ABCDE- in its value will likely not change, but I'd prefer not to depend on it.
the OID 2.5.4.97 won't change.
To summarize:
- Is there a way to look through a file from a specific line onwards? (using the
[dn]tag) - Is there maybe some alternate way how to reach said goal? (
awk? other sysutils + pipes? ...)
(my apologies for the confusing title; I don't know how to word it better)
awk. Are the key-value pairs always sepated by "key" "blank" "equal" "blank" "value" or can it be that there is no whitespace around the=sign?