0

I have a text file contains ldap data in the give below format, our default structure says every user must have its own respective "home directory" but in given below i found many users have the same home directory and that is "homeDirectory: /home/UGstudent/2013" so i want to change it to "homeDirectory: /home/UGstudent/2013/xyz" means i need to take the same name as in "uid: abc" field of each user and append it to "homeDirectory: " field.

dn: uid=abc,ou=People,dc=abc,dc=com  
uid: abc   
cn: abc def
objectClass: account  
objectClass: posixAccount  
objectClass: top  
objectClass: shadowAccount  
shadowLastChange: 15923  
shadowMax: 99999  
shadowWarning: 7  
loginShell: /bin/bash  
uidNumber: 10000  
gidNumber: 9000  
homeDirectory: /home/myworker/2013/abc  
gecos:: QWpheSBLasdfkshdfksdkfhUJTTVMxMywJU1RVLA==  
structuralObjectClass: account  
entryUUID: 74dec3aa-0fb6-4d86sfs-962e-432ea83c75e0  
creatorsName: cn=Manager,dc=abc,dc=com  
createTimestamp: 20160806091411Z  
userPassword:: e1NIQXKSJFkHkjhfskDAvS2NwYWxXcm4zY2JEL2wxYVk9  
entryCSN: 20160121043505.309136Z#000000#000#000000  
modifiersName: uid=abc,ou=People,dc=abc,dc=com  
modifyTimestamp: 20160121043505Z  

dn: uid=xyz,ou=People,dc=abc,dc=com  
uid: xyz  
cn: xyz jakas  
objectClass: account  
objectClass: posixAccount  
objectClass: top  
objectClass: shadowAccount  
shadowLastChange: 15923  
shadowMax: 99999  
shadowWarning: 7  
loginShell: /bin/bash  
uidNumber: 1260  
gidNumber: 9000  
homeDirectory: /home/myworker/2013  
gecos:: QWtzaGFqIFByZfdsfdsfdsgDAzLAlCU01TMTMsCVNUVSw=  
structuralObjectClass: account  
entryUUID: 81fc5c44-6d63-4d4a-bb74-7ae5f12bbeef  
creatorsName: cn=Manager,dc=abc,dc=com  
createTimestamp: 20160806091411Z  
userPassword:: e1NIQXkjfshgjdsJMQTB2SsdfSGFMekpZSkV1ZkZsWlE9  
entryCSN: 20150118182228.898901Z#000000#000#000000  
modifiersName: uid=xyz,ou=People,dc=abc,dc=com  
modifyTimestamp: 20160118182228Z  
  • 4
    Have you tried anything so far? By showing your attempts and describing what parts you are having difficulty with, we get a better understanding of your proficiency and can give a more precise answer. As it stands, it just looks like you've posted a requirement and want someone to write your code for you. – Ingo Karkat Apr 15 '15 at 10:03
  • i tried it using awk last time but as far i only got able to extract the users having same home directory by using command "awk 'BEGIN{FS="\n";RS=ORS="\n\n"}$14!="abcd 123"' textfile.txt". well it will be great help for me if some one write a code for me. Thanks in advance. – Sahni Apr 15 '15 at 10:09
2

A generic awk alternative:

awk '
    /^uid:/ {
        uid=$2
    }
    /^homeDirectory:/ {
        if ($NF !~ uid"$") { $NF = $NF"/"uid }
    }
    { print }
    ' text.file
  • @Sahni I'm glad I could help. – FloHimself Apr 15 '15 at 12:11
  • hello @FloHimself, I again need a bit help from the same problem, i.e. if the structure have the below lines exactly in same way : objectClass: account then structure should be modified to : objectClass: person sn: $uid $uid means to the field uid contains – Sahni Oct 8 '15 at 8:49
0

awk file :

$1=="uid" { uid=substr($2,2) ; }
$1=="homeDirectory" { printf "%s: /home/myworker/2013/%s\n",$1,uid ; next ;}
{ print ;} 

which

  • basically remember uid
  • print a fixed home dir, base on fixed year and last uid.

to be called with

awk -F: -f tmp.awk < yourinputfile

where

  • -F: use : as separator
  • -f tmp.awk use the awk file
  • thanks for the code, but the above code is changing every user's home directory instead of it i want to change homeDirectories only who have same i.e. /home/myworker/2013/ with their respective uid names. thanks again in advance – Sahni Apr 15 '15 at 11:10
0

This awk version will keep entries intact that already have a user ID as part of the homeDirectory:

awk -F ": *" '
    $1=="uid" { uid = $2 }
    $1=="homeDirectory" && $2=="/home/myworker/2013" { $0=$0"/"uid }
    { print }
' infile > outfile

(Note: your data should not have trailing spaces. To clean up that as well you can add a command { gsub(/ +$/, "", $2 } as first command of the awk program.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.