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

Karl Auer kauer at biplane.com.au
Fri Feb 19 00:06:30 UTC 2010


On Thu, 2010-02-18 at 18:02 +0000, Steve Flynn wrote:
> After completion, the entire file would consist of two
> records in this example case, both 20 bytes long.

This Perl script does the job. You might need to fix the executable
location in the first line, and you should remove the last five lines if
you don't want a possible last partial record. BTW, this will include
any extra characters from lines LONGER than 20 characters. That might
not be what you want, in which case uncomment the line after the chop()
line.

Note - tested only very minimally.

Regards, K.

#!/usr/bin/perl
$MAXLEN = 20 ;

$outline = "" ;
while (<>)
{
   $line = $_ ;
   chop($line) ;
   # $line = substr($line, 0, $MAXLEN) ;
   $outline .= $line ;
   while (length($outline) >= $MAXLEN)
   {
      print substr($outline, 0, $MAXLEN), "\n" ;
      $outline = substr($outline, $MAXLEN) ;
   }
}

# Field any leftovers.
if (length($outline) > 0)
{
   print $outline, "\n" ;
}


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)                   +61-2-64957160 (h)
http://www.biplane.com.au/~kauer/                  +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: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20100219/2346bb6c/attachment.sig>


More information about the ubuntu-users mailing list