J damne
J damne

Reputation: 72

perl / regex: how to find and replace between sets of every n characters

Say I have a very long code. I am joining lines by removing continuation characters using the normal regex

 s/<continuation chars>/\s/g

However this results in a line greater than the character limit of my compiler 17000 or the like.

How can i get this to be split between sections of 1699 characters:

I.e. find and replace ; if line length > limit ; skip next replace and begin process again

** edit **

I want to say: between newline characters, for every 1700 char (find and replace), find the next match and do not replace(ie skip), and then repeat for the next 1700..**

Upvotes: 2

Views: 203

Answers (1)

choroba
choroba

Reputation: 241808

Use length to get the length of a string. Just accumulate the total length in a variable:

#!/usr/bin/perl
use warnings;
use strict;

my $limit = 1700;

my $length = 0;
while (<>) {
    s/\n/ /;
    $length += length;
    if ($length > $limit + 1) {
        s/ $//;
        print "\n";
        $length = length;
    }
    print;
}

Upvotes: 2

Related Questions