harishnuti has asked for the wisdom of the Perl Monks concerning the following question:


Hello Monks
i have the below requirement
have to validate each line of file A to check if it exceeds 70 characters, if yes then truncate from 70th character and put it in next line.
i.e. each line a file should not exceed 70 characters
i have done something like below
#!/usr/bin/perl my $maxlen = 70; my $finalbuffer = undef; print "Check the no of characters in each line \n"; open(FILE,"textfile") or die "Unable to open the textfile \n"; while (<FILE>){ chomp; if ( length($_) > $maxlen ){ my $line = $_; while ( length($line) > $maxlen ){ my $smallchunk = substr($line,0,$maxlen-1); $finalbuffer .= qq~$smallchunk\n~; $line = substr($line,$maxlen-1,length($line)); } $finalbuffer .= qq~$line\n~; } else{ print "Line within range of $maxlen characters \n"; $finalbuffer .= qq~$_\n~; } } close FILE; open(TMP,">tmpfile") or die "Unable to open tmpfile for writing $! \n" +; print TMP $finalbuffer; close TMP;

where textfile contains below string
this line will have more than 70 characters and i should truncate from 70th character and put it in 2nd line so that each line in a file will not be greater than 70 characters which can used to send sms confirmation.

is there any better way to achieve my requirement, i mean just wanted to see if i can get more efficient method by experts.

Replies are listed 'Best First'.
Re: Line truncation and validation of no of characters
by ikegami (Patriarch) on Sep 24, 2008 at 04:54 UTC
Re: Line truncation and validation of no of characters
by repellent (Priest) on Sep 24, 2008 at 05:57 UTC
      have to validate each line of file A to check if it exceeds 70 characters, if yes then truncate from 70th character and put it in next line.
      i.e. each line a file should not exceed 70 characters

    You mean check if it has or exceeds 70 characters?
    i.e. each line should not exceed 69 characters

      my $smallchunk = substr($line,0,$maxlen-1);

    Your $smallchunk will be 69 characters or less.

    perl -lne 'chomp; print join "\n", unpack("(a69)*", $_)' textfile > tm +pfile

    FYI: Text::Wrap is a paragraph formatter, and may not break your lines at exactly after the 69th character, given $Text::Wrap::columns = 70.

    Update: Applied change - thanks, jwkrahn!

      The unpack template  "A69" will remove trailing whitespace so the string returned may be less than 69 characters long.   Better to use  "a69" as that will not modify the return string.


      Thanks for the oneliner , iam using it , really helpfull.. this is what i exactly needed..
Re: Line truncation and validation of no of characters
by gone2015 (Deacon) on Sep 24, 2008 at 10:15 UTC

    FWIW

    sub subdivide { my ($s, $n) = @_ ; $s =~ s/\G(.{$n})(?!\Z)/$1\n/g ; return $s ; } ; print subdivide("trivial\n", 12) ; print subdivide("Edge 6789012\n", 12) ; print subdivide("Over 6789012+Once\n", 12) ; print subdivide("Over 6789012+Once Edge 2\n", 12) ; print subdivide("Over 6789012+Once6789012+Twice\n", 12) ;
    gives:
    trivial Edge 6789012 Over 6789012 +Once Over 6789012 +Once Edge 2 Over 6789012 +Once6789012 +Twice


      Thanks you, iam using this, but can you please explain the function subdivide?