[olug] Another tiny Perl hack

Jay Hannah jay at jays.net
Tue Sep 2 20:04:16 UTC 2003


My friend called his local Perl support hotline (me) today. I gave his
this little Perl snippet, in case anyone is interested.

He has a file like this (LDAP ldiff):

------------
dn: uid=eshoes,ou=People,o=Lunar Gravity,c=US
accountStatus: active
deliveryMode: normal

dn: uid=ilab,ou=People,o=Lunar Gravity,c=US
accountStatus: active
deliveryMode: normal

dn: uid=oslc,ou=People,o=Lunar Gravity,c=US
accountStatus: active
deliveryMode: normal
------------

He needs to add a line of text after every dn: line. That line of text
must contain the uid from the dn: line. So, I wrote him this:

#!/usr/bin/perl
while (<>) {
   print;
   next unless /^dn:/;
   ($uid) = /uid=(\w+),/;
   print "mailmessagestore: /var/spool/maildirs/$uid\n";
}

Which spits out this output:

------------
dn: uid=eshoes,ou=People,o=Lunar Gravity,c=US
mailmessagestore: /var/spool/maildirs/eshoes
accountStatus: active
deliveryMode: normal

dn: uid=ilab,ou=People,o=Lunar Gravity,c=US
mailmessagestore: /var/spool/maildirs/ilab
accountStatus: active
deliveryMode: normal

dn: uid=oslc,ou=People,o=Lunar Gravity,c=US
mailmessagestore: /var/spool/maildirs/oslc
accountStatus: active
deliveryMode: normal
-------------

Which is what he needed. The <> operator is cool because of it's inherent
magical flexibility. If he called the program above "j.pl" (by far my
favorite program name -- I use it all the time -grin-), he could create an
output file "outputfile" either of these ways:

cat inputfile | j.pl > outputfile
j.pl inputfile > outputfile

Neat huh? Anyhoo, just another Perly two cents...

Jay Hannah
JAOPH (Just Another Omaha Perl Hacker)
Omaha Perl Mongers: http://omaha.pm.org
-- Join out mailing list! --



More information about the OLUG mailing list