manipulate text for openldap import/export question.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting manipulate text for openldap import/export question.
# 1  
Old 10-23-2009
manipulate text for openldap import/export question.

Hey guys.. I am not sure if this is the right place to post this - but here goes. I need to manipulate an openldap export to match a different schema so that I can import into that system. Basically - its just text manipulation. I have gotten alot of it done just by using simple sed, but I am sorta stuck on the following. here is an example export:


dn: mailRoutingAddress=joe.smith@domain.com,ou=People, dc=domain,dc=com
changetype: add
objectClass: user
sn: Smith
givenName: Joe
cn: Joe Smith
telephoneNumber: (304)555-5555
facsimileTelephoneNumber: (304)555-5555
title: LCM 2
uid: joe.smith

dn: mailRoutingAddress=jjones@domain.com,ou=People,dc= domain,dc=com
changetype: add
objectClass: user
sn: Jones
givenName: Jenny
cn: Jenny Jones
uid: jjones
telephoneNumber: (406)555-5555
facsimileTelephoneNumber: (406)555-5555
title: LCM 2

--snip--

There are 4000 of these entries, each with line breaks between them. What I want to do is automate changing of the dn line to get rid of the 'mailRoutingAddress bit and change it to the cn. so once the entries have been 'fixed' they will read like this:

dn: cn=Joe Smith,ou=People,dc=domain,dc=com
changetype: add
objectClass: user
sn: Smith
givenName: Joe
cn: Joe Smith
telephoneNumber: (304)555-5555
facsimileTelephoneNumber: (304)555-5555
title: LCM 2
uid: joe.smith

dn: cn=Jenny Jones,ou=People,dc=domain,dc=com
changetype: add
objectClass: user
sn: Jones
givenName: Jenny
cn: Jenny Jones
uid: jjones
telephoneNumber: (406)555-5555
facsimileTelephoneNumber: (406)555-5555
title: LCM 2



Im a little stumped at how I could do this.. I suppose I could write a for loop to go through and change the dn - but i really havent the slightest idea where to start! Any help would be appreciated.

I managed to figure out how to edit one stanza at a time (with one 'user' entry in a file called file.ldif).. like this:

#!/bin/bash
cn=`grep cn file.ldif | awk -F: {'print $2'}|cut -c2-`
sed -e "1i dn: cn=\\$cn,ou=People,dc=domain,dc=com" file.ldif

the file.dif looks like:

changetype: add
objectClass: user
sn: Jones
givenName: Jenny
cn: Jenny Jones
uid: jjones
telephoneNumber: (406)555-5555
facsimileTelephoneNumber: (406)555-5555
title: LCM 2

beforehand.. running the script outputs outputs an ldif like this:

dn: cn=Jenny Jones,ou=People,dc=domain,dc=com
changetype: add
objectClass: user
sn: Jones
givenName: Jenny
cn: Jenny Jones
uid: jjones
telephoneNumber: (406)555-5555
facsimileTelephoneNumber: (406)555-5555
title: LCM 2

How do I get it to roll through each stanza and have it modify, or create dn like this, and output it to a new (or the same) file with all the edits)?

-

---------- Post updated at 12:56 PM ---------- Previous update was at 11:26 AM ----------

So I almost have it..

Ive made a quick user list - only 2 users and tried this:

contents of users file:
dawn.powers@domain.com
pat.brown@domain.com

script:
#!/bin/bash
for i in `cat users`
do
ldapsearch -x -h ldapmaster -b "ou=people,dc=domain,dc=com" mailroutingaddress=$i cn mail | grep -Ev '#|search|result|dn' > tem
pfile
cn=`grep cn tempfile| awk -F: {'print $2'}|cut -c2-`
sed -e "1i dn: cn=\\$cn,ou=People,dc=domain,dc=local" tempfile
done

The results of running the script are:
dn: cn=Dawn Powers,ou=domain,dc=rotech,dc=local

mail: dawn.powers@domain.com
cn: Dawn Powers


dn: cn=Patricia Brown,ou=People,dc=domain,dc=local

mail: pat.brown@domain.com
cn: Patricia Brown



Notice all of the extra carriage returns? Not sure how to get rid of those.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A better way to manipulate text

Good morning everyone, I'm currently trying to convert an environment variable into a string and then attach it at the end of a command and launch it. I have the following right now, but it's very ugly: AMI_TAGS="env=test,country=XX,city=blah,galaxy=blahblah" aws ec2 create-tags... (8 Replies)
Discussion started by: da1
8 Replies

2. Shell Programming and Scripting

Array - Export/Import in global environment variables.

Hello. During startup /etc/bash.bashrc.local generates some array ..... source /.../.../system_common_general_array_env_var ..... The file system_common_general_array_env_var contains : LEAP_VERSION='42.3' ARRAY_MAIN_REPO_LEAP=('zypper_local' 'openSUSE-Leap-'"$LEAP_VERSION"'-Non-Oss' ... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Homework & Coursework Questions

DB2 Export and Import Oracle

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: is this enough to make the data perfect export into delimited file? there are some posted that i read, they... (9 Replies)
Discussion started by: Sonny_103024
9 Replies

4. Homework & Coursework Questions

DB2 Export and Import Oracle

Hi Guys, I Just wanted your opinion/ suggestion/ Help on my unix script about db2 export data with deli file and import into oracle. db2 connect to Tablename user id using psswrd db2 "EXPORT TO '/cardpro/brac/v5/dev/dat/AAAAA.DEL' OF DEL select * FROM AAAAA" db2 "EXPORT TO... (3 Replies)
Discussion started by: Sonny_103024
3 Replies

5. Solaris

Zpool import/export error

A backup/clone script of ours was recently ran. It normally only clones the rpool and renames in rpoolA. Something must've changed as it found another one of our pools that it shouldn't have. It exported that pool unbeknownst to us. Later on when a coworker realized the other pool was missing he... (2 Replies)
Discussion started by: beantownmp
2 Replies

6. UNIX for Dummies Questions & Answers

Import and export PGP/GnuPG keys

Hi, I need to export an existing PGP key and import it into GnuPG on a different machine. This is how I did the export: pgp -kx myuser _myuser_public pgp -kx myuser _myuser_private secring.skr (this is from the pgp installation directory that contains secring.skr). This produced two... (0 Replies)
Discussion started by: imchi
0 Replies

7. Shell Programming and Scripting

export / import

I have a calling script which consists of calls to other scripts via the sh command. ie vi callscript.sh sh smallscript1.sh extra unix commands sh smallscript2.sh exit In smallscript1, I prompt for a filename, which I handle via :- read f1 export f1 I then need... (5 Replies)
Discussion started by: malts18
5 Replies

8. UNIX for Dummies Questions & Answers

Question on how to manipulate a SIMPLE text file (using awk?)

I have a simple txt files that looks something like this (The title is a part of the text file) Student Grades --------------- 1 Tim Purser 89 2 John Wayne 56 3 Jenn Hawkins 95 4 Harry Potter 75 Here are my questions: How would I ONLY print the names of students... (2 Replies)
Discussion started by: ninjagod123
2 Replies

9. Solaris

Export/import ZFS ACL's

I've been wondering about this one, is there any way to do the following with ZFS ACL's (i.e. "copy" the ACL over to another file)? getfacl /bla/dir1 | setfacl -f - /bla/dir2 I know about inheritence on dirs, it doesn't work in this scenario I'm working on. Just looking to copy the ACL's. ... (3 Replies)
Discussion started by: vimes
3 Replies

10. Filesystems, Disks and Memory

Local NFS import / re-export using automount?

Hi, Can I do the following: On SunOS 5.8 /etc/vfstab: remote-host:/Volumes/webdata - /export/home/webdata nfs - yes rw,vers=3,soft,intr,bg,timeo=600 In /etc/auto_direct: /home/science $HOST:/export/home/webdata/science /home/science-edu ... (2 Replies)
Discussion started by: bloyall
2 Replies
Login or Register to Ask a Question