Fellow monks,
I've run into something that might result in serious hair loss. Here's what:
The setting
I'm working in a 2Gb+ file in LDIF format (for an LDAP user database I'm populating) where I need to replace parts of the DN's of the users. E.g.:
cn=Perl Monks,ou=Dining Hall,ou=Monastery,c=Universe
needs to become:
cn=Perl Monks,ou=Bedchambers,ou=Monastery,c=Universe
Not a problem, I says, just search and replace the thing, and await happiness, joy and bliss everafter. Well, no. It turns out that the input file has a line length of 80 chars, and enforces it: there's a newline character in pos 80 if the line is longer than 80 charachters. Very inconvenient, of course, as for long DN's this might mean that it actually says:
cn=Perl Monks,ou=Dining H\nall,ou=Monastery,c=Universe
And those things are not matched by a simple regular expression, as the newline can appear just about anywhere in the DN (apparently, this is not a problem for the tools I will use later down the line to import the users).
Some solutions
Some things I can think of doing to remedy this:
- Doing it the hard way. Build my search string with \n{0,1} in between every character.
- Strip \n in the middle of a DN (that's easy: next line does not start with \w+: ), but I'm not sure that this will not muck up my importing of said users later - there has to be a reason why the combination of tools I need to use generates 80-wide lines.
- Do something clever with regular expressions, which brings me to...
The question
I have this nagging feeling that I am missing a deceptively simple, yet cunningly clever way of doing this with a regular expression. Can any monk bring some enlightenment in this?
CU
Robartes-
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.