23

Ldapsearch without all the line wrapping

Posted January 8th, 2011 in Networking by Dieter

Anyone who have ¬†used ldapsearch (you know, that handy dandy tool to query your LDAP-database) in a script, will eventually¬†notice one big problem. For reasons beyond my understanding, the original author of this tool decided that it would be cool to apply line wrapping when output is generated with lines over 76 characters. Guess what, it’s not… I searched on the almighty Internet for a solution and found several, but the following I liked the most.

perl -p00e 's/\r?\n //g'
 
# Example
ldapsearch -xLLL -h oh.mighty.ldapserver -b dc=example,dc=com | perl -p00e 's/\r?\n //g'

Why? It’s short, does it’s job fast and perl is widely supported. I found this little gem on a mailinglist, but because it’s so easily overlooked, I posted it here.


23 Responses so far.

  1. Szabolcs says:

    It is working. Thank you!

  2. J says:

    Dieter, thanks so much for reposting this. This little perl tweak was just the thing I needed for a couple of my ldapsearches that I could get close, but never quite right… It works great and fast.

  3. rob harris says:

    I’d like to reiterate last message…and a big thankyou!I am not a perl programmer but found it very helpful and easy to use and it really sorts out my wrap problem

  4. Andy says:

    I would also recommend setting up an alias for this. You can add it to your .profile or .bashrc file. That way you only need to remember ldapsearch -p … dn | fullLine

    Here is the alias command I setup.
    alias fullLine=’perl -p00e “s/\r?\n //gi”‘

  5. Trevor Sturdevant says:

    In Bash v3 and later you can make use the builtin regex replacement in bash to accomplish the same thing without forking a huge forking perl process.

    # Example
    RESULTS=`ldapsearch -xLLL -h oh.mighty.ldapserver -b dc=example,dc=com`
    RESULTS=${RESULTS/$’\r’$'\n’ /}
    RESULTS=${RESULTS/$’\n’ /}
    echo “$RESULTS”

  6. Kerry Tholl says:

    Quick hit and DONE! works like a charm, THANK YOU!

  7. ketzaldev says:

    Dieter, you’re the man… Thanks a lot, saved hours of rewriting for a 100 000 entries LDAP ;)

  8. Prem says:

    you could also pipe the output of the ldapsearch to sed with the following sed command to concat lines

    sed -n -e ’1{h;d’} -e ‘/^ /{s/^ //;H;b};/^ /!{x;s/\n//;p;d}’

  9. Andrew Schulman says:

    Cool, thanks Dieter. Much easier than the look-ahead parsing I was doing.

  10. Rob Thomas says:

    BTW, there is an option (-T) to disable line wrapping. Probably better to use that than to try to cludge around it.

    (And yay, you’re the first google hit for this question)

  11. Michael says:

    Another happy camper. Thanks!

  12. oernii says:

    Thanks!

    btw: the -T options does not work here (centos6)

  13. Mike says:

    this did the trick , you are awesome

  14. Steini says:

    ldapsearch in debian wheezy also has an option to disable wrapping.

    -o ldif-wrap=no

  15. Mark says:

    ldapsearch with ‘-o ldif-wrap=no’ works for openldap-2.4.28 (a 2013 build I think, for debian)

    -o [=<optparam] general options
    nettimeout= (in seconds, or “none” or “max”)
    ldif-wrap= (in columns, or “no” for no wrapping)

  16. rjc says:

    `-o ldif-wrap=no` does exactly what you are looking for :^)

  17. Serhiy says:

    You can use key: -o ldif-wrap=no

    ldapsearch.exe –help
    ~~~
    -o [=] general options
    nettimeout= (in seconds, or “none” or “max”)
    ldif-wrap= (in columns, or “no” for no wrapping)

  18. David says:

    Did you hear about ldapsearch options ldif-wrap?

    You can use simple:
    -o ldif-wrap=no

  19. MTecknology says:

    Just use: -o ldif-wrap=no

  20. Endzone says:

    I simply use

    ldapseach -o ldif-wrap=no …

  21. oscar says:

    just for completeness sake:

    ldapsearch -QY EXTERNAL -H ldapi:/// -b cn=config -o ldif-wrap=no

    -o ldif-wrap=no being the relevant part.

  22. Franky G says:

    Thought id add another one….

    | awk ‘RS=”\n “; ORS=”" {printf “%s %s %s %s\n”, $1,$2,$3,$4}’

  23. Stephan says:

    Try this:
    ldapsearch -o ldif-wrap=no

Leave a Reply to David