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

Hi, I'm trying to insert newline paragraph breaks every 5 periods (sentences), in a text file. I'm getting a blank output, no errors. Thanks in advance.

perl -e '$count = 0; s/\./(++$count % 5 == 0)?"\n":$&/ge;' text-blob.txt > paragraphs.txt

Replies are listed 'Best First'.
Re: Newline after every 5 periods
by kcott (Archbishop) on Jul 20, 2016 at 06:19 UTC

    G'day paulm2,

    Welcome to the Monastery.

    You're not reading any input or writing any output.

    See the -n and -p options in perlrun.

    You also appear to be labouring under the false impression that periods map directly to sentences; for instance, here's one sentence (with three periods):

    "Apples cost $1.50 and pears cost $2.50."

    — Ken

      Thanks, good to be here.
Re: Newline after every 5 periods
by Anonymous Monk on Jul 20, 2016 at 04:47 UTC
    perl -p056e '$_ .= "\n" x !(++$c % 5)' text-blob.txt > paragraphs.txt

      Thank you, that works! What's the 0056 doing?

      I'd also like to randomize these paragraph breaks in a range of 5 - 19 periods (not just every n periods), and output to new file. Here's what I've got; the while loop/output isn't right. If it's more appropriate to edit my initial post or make a new post I will. Thanks.

      #!/usr/bin/perl use strict; use warnings; my $filename = 'input.txt'; my $out = 'ouput.txt'; my $minimum = 5; my $range = 19; my $random_number = int(rand($range)) + $minimum; open(my $fh, '>>', $filename) or die "Could not open file '$filename +' $!"; while(my $fh) { print my $fsh('$_.="\n\n" x !(++$c % $random_number')); #compilatio +n error }; close $fh;
        > What's 056 doing?

        See perlrun for the -0 option, and

        perl -wE 'say "\056"' .

        for the meaning of 56.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        You seem to have changed your post above by deleting some or all of the original question: What's 056 doing?

        This makes choroba's reply seem incoherent at best, since he seems to be answering a question that no one has asked.

        Please don't change or delete material in your posts in a way that destroys context. Please see How do I change/delete my post? to learn the ways of righteous alteration.


        Give a man a fish:  <%-{-{-{-<

      Can use the $. also. E.g., and not caring for uninitialized warnings:

      perl -p056e '$_ .= ("\n")[$. % 5]'

        Thanks, any suggestion how to implement rand in this statement for \n break within a range of character (.) occurrences?