AWK experts - how would I code around this in awk...

Karl Auer kauer at
Fri Feb 19 14:35:33 UTC 2010

On Fri, 2010-02-19 at 13:35 +0000, Dave Howorth wrote:
> Here's another perl version:
> #!/usr/bin/perl
> use strict;
> use warnings;
> local $/ = '';
> my $text = <>;
> $text =~ s/\n//g;
> $text =~ s/(.{20})/$1\n/g;
> print $text;

Cool! But:
 - it reads the entire contents of stdin into memory
 - there may be problems with arbitrarily large variables
 - a partial last record will not be terminated with a newline
 - it doesn't handle blank lines

And the OP did specifically exclude such solutions from the game :-)

You can fix the blank lines by setting $/ to "undef" rather than an
empty string.

For the non-Perlers amongst us, Dave's program loads the entire input
into a variable in one hit, then removes all the newlines from the
variable, then inserts a newline after every 20th character in the
variable, then writes the variable out.

Regards, K.

Karl Auer (kauer at                   +61-2-64957160 (h)                  +61-428-957160 (mob)

GPG fingerprint: B386 7819 B227 2961 8301 C5A9 2EBC 754B CD97 0156
Old fingerprint: 07F3 1DF9 9D45 8BCD 7DD5 00CE 4A44 6A03 F43A 7DEF
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <>

More information about the ubuntu-users mailing list