[RndTbl] ldapsearch(1) word wrap

Adam Thompson athompso at athompso.net
Sat Aug 13 14:57:56 CDT 2011


If you're using LDAP, you've probably run into an annoying feature of the 
LDAP standard: all LDIF-compliant output shall be wrapped at 80 characters 
or less.  The LDAP standards explicitly specify how wrapping shall take 
place, but the column width varies from one program to another.
Some implementations of ldapsearch(1) accept the "-T" option to turn off 
wrapping, or "-r 0" to specify wrapping at column #infinity.  OpenLDAP's 
version, however, does not accept any such option.

There's an easily-discoverable (i.e. googleable) way to remove the 
peculiar line wrapping, by running

	ldapsearch [all the various options] | perl -p00e 's/\r?\n //g'

which works equally well under Windows or under UNIX.
Since I hate typing in a command like that over and over, I thought to use 
a shell alias to make my life easier.  Shell aliases, as I discovered, do 
not permit parameter substitution.  So, when I tried this:

	alias lds='ldapsearch "$@" | perl -p00e "s/\r?\n //g"'

it didn't work.  The solution is to use a shell function, and this is now 
what's in my .bashrc (I removed the \r? which took care of DOS EOLs, since 
I'm not running DOS here):

	lds() { ldapsearch "$@" | perl -p0e 's/\n //g' }


In perl 5.8.8, at least (which ships with RHEL/CentOS 5.6), the default 
option to '-0' is \000, so specifying -00 is redundant.  And possibly 
incomplete anyway, since it's supposed to be octal (3 digits).  I hated 
the thought of exec'ing perl just to do this, but it turns out there's no 
way to do it with sh, sed, awk, or tr because we're manipulating newline 
characters.  Oh, well, perl's been in production use since ~1992, it 
should be stable by now.

Note: yes, it is probably *possible* to do it in shell or awk by 
specifying a replacement IFS, something like:

	lds() { ldapsearch "$@" | read -d '\0' X; echo ${X/\n /g}; }

but it didn't work for me after about 15 minutes of fiddling and testing. 
On the other hand, even bash doesn't like single variables exceeding about 
32KB, and some LDIF output can be huge.  At least with perl you can 
process LDIF output up to the size of your remaining VM (eventually). 
There are specialized binary stream editors (like bbe-) that might be more 
efficient but those aren't typically already installed.

Some people post junk like this to their blog; I'm old-school, I figure a 
mailing list that Google indexes is good enough for me :-D.

-Adam Thompson
 athompso at athompso.net





More information about the Roundtable mailing list