#! perl5 # # wrap.pl Daniel Brockman 20030407 wraps lines of text file # # Usage: wrap.pl [nn] outfile # where nn>0 is the number of characters per line, # excluding trailng "\n", defaulting to 60. # ################################################ # The author grants permission to use for any # # purpose, including copying and modification, # # provided the user acknowledges the author by # # name. # # -- Daniel Brockman, author, 20031220 # ################################################ # # wrap.pl reads sequentially through the input files. # wrap.pl trims trailing whitespace from each line it reads. # wrap.pl replaces consecutive interior whitespace with # a single space. # If wrap.pl encounters a word longer than nn chars, then # wrap.pl breaks it into two words, the first of which is # the first nn chars. The second word contains the # remaining chars. wrap.pl writes the first word on a new # line, then considers the 2nd word as a newly encountered # word. # If wrap.pl encounters a line longer than nn chars, then # wrap.pl finds the last whitespace char before the # nnth char, and breaks the line just after that # whitespace, resulting in two lines. wrap.pl trims # leading and trailing whitespace from the first line # and writes the first line, then considers the 2nd line # as a newly encountered line. # If wrap.pl encounters a line no longer than nn chars, # then wrap.pl writes that line. # # Note: To normalize a file, precede wrap.pl with unwrap.pl, # being aware that special formatting may not survive # unwrapping and wrapping. # # Example: # perl -- unwrap.pl < input > intermediate # perl -- wrap.pl 45 < intermediate > normalized # # my( $line, # current line $linf, # next line $fracword, # fractional word on end $ll, # length of line $lw, # length of word $pw, # char pos of lws (last white space) $nn, # chars per line $n, # number of lines read from file @chz, # chars in line $flag, # whitespace found $i, # aux ); $n=0; # init $linf=""; # init $nn=60; # default 60 chars per line if($#ARGV>=0){ $nn=$ARGV[0] ; # chars per line specified shift; } while($line=<>){ # read a line from file $n++; # incr count $line=~s/\s+$// ; # trim trailing whitespace $line=~s/\s+/\ /g ; # replace consecutive interior \s if($line=~m/^$/){ # $line empty? print "\n"; # print blank line } while($line ne ""){ # while nonempty $line $line=~s/\s+$// ; # trim trailing whitespace $line=~s/^\s+// ; # trim leading whitespace $ll=length($line); # measure line if($ll>$nn){ # long line? @chz=split(//,$line); # get indiv chars for($i=$nn-1;$i>=0;$i--){ # loop the chars if($chz[$i] eq " "){ # white space? last; # leave loop } } # end loop the chars $pw=$i ; # mark lws if($pw<0){ # long word? $pw=$nn ; # divide at wraplength } $linf=substr($line,$pw+1) ; # divide line $line=substr($line,0,$pw) ; $line=~s/\s+$// ; # trim trailing whitespace $line=~s/^\s+// ; # trim leading whitespace } # end if long line print "$line\n"; # write the line $line=$linf ; # remaining unwritten line $linf="" ; # reinit } # end while nonempty line } # end while read a line from file #