#!/usr/bin/perl my $file = shift; my $new_length = shift; open (DAT, $file) || die "couldn't open the file!"; my $raw_data = ''; while (){ chomp; $raw_data .= $_; } my ($offset, $end) = (0, $new_length); LINE: while ($end < length($raw_data)) { if (substr($raw_data,$end,1)=~m/\s/) { substr($raw_data,$end,1,"\n"); $offset = $end + 1; $end = $offset+$new_length; next LINE; } else { # backtrack $end--; while ($end > $offset) { if (substr($raw_data,$end,1)=~m/\s/) { substr($raw_data,$end,1,"\n"); $offset = $end + 1; $end = $offset+$new_length; next LINE; } --$end; } # No whitespace split in mid word $end = $offset+$new_length; substr($raw_data,$end,1,"\n"); $offset = $end + 1; $end = $offset+$new_length; next LINE; } } $raw_data .= "\n" unless $raw_data=~m/\n$/; print $raw_data;