I have a file which contains the userName and its associated information as i provided in the user_list.txt file. There are two things in the data:
- data always starts with the keyword - dn:and its associated values like- mail,- givenName,- uidseparated by individual newline.
- However, in some cases it it only starting line only which starts with - dn:.
I have below code which partially works, saying that it doesn't prints the last line dn: uid=aadhar,ou=people,o=udalt.com as you see in the output. Just wondering what i'm doing wrong here, and would appreciate any correction or advice from the experts.
#!/grid/common/pkgs/python/v3.6.1/bin/python3
myline = ''
Flag = 0
with open('user_list.txt', 'r') as frb:
    for line in frb:
        if line.startswith("dn:"):
            Flag = 1
            if Flag == 1:
                print(myline)
            myline = line.strip()
        else:
            myline = myline.strip("\n") + ' ' + line.strip("\n")
            Flag = 0
File which i'm trying to get results from:
$ cat user_list.txt
dn: ou=People,o=udalt.com
dn: ou=DC,ou=People,o=udalt.com
dn: uid=3dv,ou=people,o=udalt.com
givenName: DUMAN reserved
displayName: DUMAN reserved account
uid: 3dv
dn: uid=aabdalla,ou=people,o=udalt.com
mail: aabdalla@udalt.com
givenName: kamina
displayName: kamina Abdal
uid: aabdalla
l: Vendor Loc, US
dn: uid=aabhiram,ou=People,o=udalt.com
mail: aabhiram@udalt.com
givenName: Amperayani
telephoneNumber: +91-8888888888
displayName: Amperayani Abhiram
l: Bangalore, India
uid: aabhiram
dn: uid=aadhar,ou=people,o=udalt.com
Code Result Below:
dn: ou=People,o=udalt.com
dn: ou=DC,ou=People,o=udalt.com
dn: uid=3dv,ou=people,o=udalt.com givenName: DUMAN reserved displayName: DUMAN reserved account uid: 3dv
dn: uid=aabdalla,ou=people,o=udalt.com mail: aabdalla@udalt.com givenName: kamina displayName: kamina Abdal uid: aabdalla l: Vendor Loc, US
dn: uid=aabhiram,ou=People,o=udalt.com mail: aabhiram@udalt.com givenName: Amperayani telephoneNumber: +91-8888888888 displayName: Amperayani Abhiram l: Bangalore, India uid: aabhiram
Thanks for the help and guidance in advance.
 
     
    