Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jarthur!bridge2!cook.ESD.3Com.COM!marcl From: marcl@ESD.3Com.COM (Marc Lavine) Newsgroups: comp.lang.perl Subject: Re: Splitting by character count in perl? Message-ID: Date: 8 Mar 91 08:27:25 GMT References: <17806@milton.u.washington.edu> <1991Mar7.002047.5121@uvaarpa.Virginia.EDU> Sender: news@bridge2.ESD.3Com.COM Lines: 34 Nntp-Posting-Host: cook.esd.3com.com djo7613@milton.u.washington.edu (Dick O'Connor) writes: >Our old program copied the first 78 characters on a line to the output file >after prepending and appending the character 'A'. Characters 79-150 were >written to line 2 of the output file with prepended and appended 'B'. A >simple routine glued things back together at the other end. eichin@athena.mit.edu (Mark W. Eichin) writes: > As for your particular example, the one-liner: >perl -ne '@two=unpack("a78a72",$_); print "A",$two[0],"A\nB",$two[1],"B\n";' I just started hacking with Perl last week (and think it's great -- thanks for another wonderful tool, Larry), but I'm a long-time fan of regular expressions (in the distant past, I used to edit files with "ex"). I really like having Perl's "fancy" regular expressions available. So, here's a different solution to the problem using only regular expressions (which should be quite fast): To split the lines use: perl -pe 's/^(.{78})(.{72})$/A\1A\nB\2B/' And to join them use: perl -pe 's/^A(.{78})A\n/\1/; s/^B(.{72})B$/\1/;' (which came out very similar to Randal Schwartz's suggestion of: perl -pe 's/^A(.*)A\n$/$1/ || s/^B(.*)B\n$/$1\n/;' ) BTW, I came up with the following motto for Perl: Perl: Kitchen sink included. -- Marc Lavine Broken: marcl%3Com.Com@sun.com Smart: marcl@3Com.Com UUCP: ...{sun|decwrl}!3com.3com!marcl