John W. Krahn wrote: > > Daniel Liston wrote: > > > > Here is a tool I use to unfold long lines in LDAP outputs. > > It also works on mailboxes that have the Received: lines > > or Content-*: lines folded. > > There are two things in your code that may cause a problem. > > > > #!/usr/bin/perl > > > > #syntax: unfold.pl filename > newfilename > > > > if ($ARGV[0]) { > > local $/ = ''; > > open(FILE, "<$ARGV[0]") or die "can't open $ARGV[0]: $!\n"; > > @records = <FILE>; > > close(FILE); > > } > > foreach $record (@records) { > > $record =~ s/\n\s//g; > > print "$record\n\n"; > > } > > You are removing a newline and a single whitespace character. If your > input looks like this: > > one > two > three > > Your substitution will result in: > > one two three > > And if your input looks like this: > > one > two > three > > Your substitution will result in: > > onetwothree > > However if you change the substitution it will screw up the record > separator. You should chomp the records when you read from the file and > substitute all whitespace after the newline with a single space. > > #!/usr/bin/perl > > #syntax: unfold.pl filename > newfilename > > @ARGV or die "usage: $0 filename\n"; > > my @records = do { > local ( $/, *FILE ) = ''; > open FILE, $ARGV[0] or die "can't open $ARGV[0]: $!"; > grep chomp, <FILE>;
This will drop the last line of the file if there is no terminating separator. grep {chomp, 1} <FILE>; > > foreach my $record ( @records ) { > $record =~ s/\n\s+/ /g; > print "$record\n\n"; > } > > ...and even then the original problem was that the input file doesn't contain empty lines, just blank ones with whitespace before the record separator. Setting $/ = '' won't work in this case, and the record split has to be coded explicitly. If only you could write $/ = qr/\n(\s*\n)+/ then all would be well. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]