0

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:

  1. data always starts with the keyword dn: and its associated values like mail, givenName, uid separated by individual newline.

  2. 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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53
  • 1
    Seems like you just need to `print(myline)` when exiting the for-loop. That might be a good use of [`for`-`else`](https://stackoverflow.com/a/9980752/4518341). – wjandrea Aug 04 '19 at 14:48
  • @wjandrea, thanks for the input, would mind putting that as a answer to test. – Karn Kumar Aug 04 '19 at 14:59

1 Answers1

1

I believe this will solve your problem:

Code:

dataBlock = ''
with open('user_list.txt', 'r') as frb:
    for line in frb:
        line = line.strip("\n")

        if line.startswith('dn:'):
            print(dataBlock)
            dataBlock = ''

        dataBlock = dataBlock + ' ' + line
    else:
        print(dataBlock)         

Output:

 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

Explanation: For each line in the user_list.txt file, it checks if that line starts with a 'dn:'. If it does then it prints the current data block and resets the data block with an empty string. It also adds the current line to the data block for each line.

Finally, as the last line won't get printed by the for loop, the else block will print the last block.


Thanks.

gjzim
  • 593
  • 4
  • 11
  • @-Gul Jamal Zim, thanks for the answer and explanation, i'm testing this out +1 – Karn Kumar Aug 04 '19 at 15:00
  • Can you explain , how the the lines associated with the `dn:` like `givenName` `uid` etc are evaluated and then joined? – Karn Kumar Aug 04 '19 at 15:03
  • As you said that data always starts with the keyword `dn:` and is followed by `mail`, `givenName`, `uid` in individual newlines. That basically means if a line starts with `dn:`, it starts a new data block. So, we don't actually need to check the lines that follow the `dn:` line. We can just add those to the data block. However, if you need to check/evaluate `givenName`, `uid` you can add those checks after the `if line.startswith('dn:'):` block and validate those. – gjzim Aug 04 '19 at 15:12
  • Jamal, thanks , that's make sense. Appreciate your help. – Karn Kumar Aug 04 '19 at 15:34