#! perl5 # # unwrap.pl Daniel Brockman 20030407 unwrap text lines # # Usage: unwrap.pl { {< file } | { file [...]} } > outfile ################################################ # 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 # ################################################ # unwrap.pl removes all newlines between nonempty lines. # unwrap.pl replaces multiple empty lines with a single # empty line. unwrap.pl ignores empty lines preceding the # first nonempty line in the file. # unwrap.pl reads the file sequentially, searching for a # nonempty line. On encountering a nonempty line, unwrap.pl # appends succeeding nonempty lines to the first one in the # set, replacing intervening white space with a single # space. On encountering an empty line or the end of the # file, unwrap.pl writes the concatenated line followed by # an empty line. Then unwrap.pl repeats the process, # searching for the next nonempty line. my ( $line, # current line $lind, # previous line $slug, # aux $n, # count of lines read ); $lind=""; # init prev line $n=0; # init count while($line=<>){ # while read new line $n++; # incr count chop($line); $slug=$line; # avoid trashing line $slug=~s/[ \t\n]+// ; # remove white space # print "AAA n:$n slug:$slug\n"; if($slug eq ""){ # empty line? if($lind ne ""){ # prev line also empty? print "$lind\n"; # print prev line print "\n"; # print empty line $lind=""; # reinit prev line } }else{ # nonempty line? $line=~s/[ \t\n]+$// ; # trim trailing white space if($lind eq ""){ # prev line empty? $lind=$line; # cur line becomes prev line }else{ # prev line not empty? $line=~s/^[ \t\n]+// ; # remove leading white space $lind.=" ".$line ; # append current line } } # end if slug eq "" } # end while read new line # end of file print "$lind\n"; # print prev line print "\n"; # print empty line #