Fedora Linux Support Community & Resources Center
  #1  
Old 23rd October 2009, 02:40 PM
i2ambler Offline
Registered User
 
Join Date: Aug 2006
Posts: 20
linuxubuntufirefox
Scripting/text manipulation help

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

-
Reply With Quote
  #2  
Old 23rd October 2009, 04:40 PM
i2ambler Offline
Registered User
 
Join Date: Aug 2006
Posts: 20
linuxubuntufirefox
okay, so I managed to figure out how to edit one stanza at a time.. 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


this outputs the ldif file with the correct cn information.. however - How do I get it to roll through each stanza and have it modify the dn like this? or am i barking up the wrong tree..
Reply With Quote
  #3  
Old 23rd October 2009, 05:50 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 9,041
linuxfedorafirefox
Hmm - I'm stumped.

I would use emacs. You can create a kb macro w. Meta=( and Meta-) then execute it releatdely with esc-E (IIRC).
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #4  
Old 23rd October 2009, 06:44 PM
i2ambler Offline
Registered User
 
Join Date: Aug 2006
Posts: 20
linuxubuntufirefox
I dont know either.. the previous admin did something with perl to do something similar, but he is gone and so are his perl scripts. I dont know jack about perl.

lol, ive not used emacs in years.. I didnt know if there was a way to programatically loop through each stanza and replacing the first line of each stanza with that stanza's cn value + the rest of the dn.


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 combat this.. but I think I am almost there!
Reply With Quote
  #5  
Old 23rd October 2009, 07:09 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,896
linuxfedorafirefox
Actually, it would be simpler to make the whole thing in perl:

Code:
#!/usr/bin/perl

while (<>){
    if (/^$/) {	# handle the end of the stanza
	$stanza[0] =~s/mailRoutingAddress=.*,ou=/${name},ou=/;
        print @stanza,"\n";
	@stanza = ();
        $name="unknown";
    } else {
	if (/^cn: (.*)$/) {
	    $name = $1;
	}
	push(@stanza,$_);
    }
}
Now you just redirect your input file into the script, and redirect output to a new file...
Oh, and make sure there is a blank line at the end of the input file as that is how the last
stanza should be terminated.

The "unknown" name is there just in case there is a missing "cn:" field. Better the name
be unknown than the wrong name.

Let me know if you would like more comments... it was a quickie.

One more thing - if there were blanks in front of the "cn" then that too can be handled
(replace the pattern /^cn: ..., with /^\s+cn:... maybe just removing the ^ will do).

If you have data files like this it is easier to read here if they are in
"opensquarebracket"code"closesquarebracket" and
"opensqurebracket"/code"closesquarebracket" sequences..

It is surprising at how often I see perl being underrated in favor of things like python... :-)

Last edited by jpollard; 23rd October 2009 at 07:37 PM.
Reply With Quote
  #6  
Old 24th October 2009, 02:51 AM
ghostdog74 Offline
Registered User
 
Join Date: Sep 2006
Posts: 52
linuxfedorafirefox
that's also easy to do, without Perl. Just gawk
Code:
#!/bin/bash
awk 'BEGIN{  RS=""; OFS=FS="\n";}
{
 for(i=1;i<=NF;i++){
    if($i~/^cn/){ save=$i}
 }
 gsub(":","=",save)
 gsub(/mailRoutingAddress=.*.com,/,save",",$1)
 print 
 print ""
}'  file
Reply With Quote
  #7  
Old 24th October 2009, 04:48 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,768
linuxfirefox
Quote:
Originally Posted by ghostdog74 View Post
Code:
#!/bin/bash
awk 'BEGIN{  RS=""; OFS=FS="\n";}
{
 for(i=1;i<=NF;i++){
    if($i~/^cn/){ save=$i}
 }
 gsub(":","=",save)
 gsub(/mailRoutingAddress=.*.com,/,save",",$1)
 print 
 print ""
}'  file
Just one small problem with this, it puts a space after the = in the cn part of the dn. For example it gives
Code:
dn: cn= Joe Smith,ou=People,dc=domain,dc=com
when it should be
Code:
dn: cn=Joe Smith,ou=People,dc=domain,dc=com
That can create problems in LDAP. So just modify the first gsub line to fix that:
Code:
gsub(": ","=",save)
Reply With Quote
  #8  
Old 2nd November 2009, 05:29 PM
i2ambler Offline
Registered User
 
Join Date: Aug 2006
Posts: 20
linuxubuntufirefox
The gawk script worked perfectly.. Thanks for the help. Sorry for the late response, I was in a vmware class all last week.
Reply With Quote
  #9  
Old 11th November 2009, 12:57 PM
kimon alawoya Offline
Registered User
 
Join Date: Nov 2009
Posts: 1
windows_xp_2003ie
uploading matlab files into scilab

hi to all,

can anybody please explain to me how to upload a matlab file onto scilab. i have had a look at the scilab help file but it not very clear. i would be greatful for your help. thanks
Reply With Quote
Reply

Tags
manipulation, scripting or text

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Text file manipulation with bash tutorial BeerGut Using Fedora 11 24th October 2009 06:37 AM
cron scripting/bash scripting coolbeansdude51 Using Fedora 7 16th January 2007 04:13 AM
Bash scripting help - printing text on screen old-school style tonyingesson Linux Chat 1 4th October 2006 10:44 PM


Current GMT-time: 00:54 (Saturday, 20-12-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
...Creek Side - Stockholm-Arlanda Airport (ARN) Travel Photos on Instagram - Marina of Koper Travel Photos - Te Whiti Park Instagram Photos - Texas State Fair Photos on Instagram