in reply to Re^4: Perl File Manipulation
in thread Perl File Manipulation

My only problem now is I have 4 records with cn on the last line of the record and for some reason its not find it in the search.

If you're using the code that I originally proposed, then the match is failing because there is nothing that matches the "$3" ("$nexttag") portion of the regex. If you make that part of the match optional, it should work fine -- just put a "?" quantifier on the part that would be $3:

if ( /(\ncn: (\S+))\n(\S+)?/ ) {
Now, if you need to make sure that all the lines are in a specific order (e.g., if the "cn:/uid:" pair should not be the last two lines of the record), you would probably be best off splitting the record on "\n" (after you've fixed the uid line), assigning the lines to an array (or better yet, a hash, keyed by the first field on each line), and then printing the lines out in the desired order.