Reputation: 11
I have an LDAP Schema file (Text file written in BASH) read into a List in Python. My Code:
LDAP_SCHEMA_LIST = []
LDAP_CORE_LIST = []
LDAP_CORE_DICT = {}
LDAP_CORE_FILE = '/../../../schema-core'
with open (LDAP_CORE_FILE) as LDAP_FILE_OBJECT:
LDAP_SCHEMA_LIST = LDAP_FILE_OBJECT.readlines()
for line in LDAP_SCHEMA_LIST:
if re.match("[A-Za-z0-9]+:", line)
print(line.strip())
Output:
dn: cn=<OU>, cn=schema, cn=config
objectClass: schemaConfig
cn: <OU>
objectAttribute: {n} ( 1.2.3.4.5.6 Name Address Us
er Just making up some more data to fill 1.1..1.1
.111.131.111 SINGLE-VALUE )
objectAttribute: More Plaintext Data
This would be the string above continued on this line
1212 this is another continued line from the string
objectAttribute: etc...
I would like to be able to create a regex search pattern or sed/awk statement to match the two lines of the continued string and join them as one line. For Example:
dn: cn=<OU>, cn=schema, cn=config
objectClass: schemaConfig
cn: <OU>
objectAttribute: {n} ( 1.2.3.4.5.6 Name Address User Just making up some more data to fill...
objectAttribute: More Plaintext Data This would be the string above continued on this line 1212 this...
Upvotes: 0
Views: 158
Reputation: 781751
Save each line in a variable before printing it. When you read a line, check if it begins with a space, indicating that it's a continuation of the previous line. If it does, concatenate the line to the variable. Otherwise, print the saved variable and assign the variable with the current line.
At the end, print the last saved line.
saved_line = None
for line in LDAP_SCHEMA_LIST:
line = line.strip()
if line.startswith(" "):
saved_line += line
else:
if saved_line:
print(saved_line)
saved_line = line
if saved_line:
print(saved_line)
Upvotes: 1