Group: Perl Utility to Groupify Text

By Jari Perkimki

The summary

This tool is suitable for massaging e.g. ciphertext into fixed-width groups. See: Affine Cipher, Vigenre Cipher

The syntax

Usage: ./group [-b] [-g] [-r] [-u] [-w] file1 [file2 ...]
Group: a grouping utility.
Options:
-b       Do a bitwise complement. Pretty unstable in extended ASCII. Use with caution.
-g       Turn the input into 5-character-long groups.
-w N     Make N-character-long groups. Meaningful only together with -g.
-u       Make all text uppercase. Default: lowercase.
-r       Reverse the text direction.

Files: Accepts multiple files as argument. No files means using STDIN.

The code

#!/usr/bin/perl
#
# Initial release 16.3.2006. jpe@uwasa.fi
#

undef $/;
$gw = $lw = $bit = $groups = $rev = $ucase = 0;
$a = "";

use Getopt::Long;
GetOptions ('b'   => \$bit,     # bitwise complement
            'g'   => \$groups,  # groupify
            'r'   => \$rev,     # reverse string
            'u'   => \$ucase,   # uppercase, default: lowercase
            'w=i' => \$gw);     # width of a group, default: 5

if ($gw < 1) { $gw = 5; }                # set default group width
$lw = int(80 / ($gw + 1)) * ($gw + 1);      # max line length

while(<>) {

    s/\s+//g;
    $a = $_;

    if ($rev) { $a = reverse($a); }
    if ($bit) { $a = ~$a; }

    if ($groups) {
        $a =~ s/(.{$gw})/\1 /g;
        $a =~ s/(.{$lw})/\1\n/g;
    }

}

$ucase ? $a =~ tr/a-z/A-Z/ : $a =~ tr/A-Z/a-z/ ;
print "$a\n";

Examples of use

1. Easy start

The easiest use -- which does absolutely nothing, besides stripping off all whitespace and changing the output to lowercase -- is:

echo "This does absolutely nothing" | ./group

thisdoesabsolutelynothing

2. Grouping (-g, -w and -u)

Use this tool together with a cipher tool such as affine.pl to make the output to look "professional":

./affine.pl ipsum.txt | ./group -g

lorem ipsum dolor sitam etcon secte tuera dipis cinge litdo necpo suere elita
cfeli smaec enasi psumv estib ulumd apibu slaor eetfe lisdo necfa cilis ismol
lisqu amins ollic itudi narcu etleo inhac habit assep latea dictu mstae neanv
eldol orbla nditl oremg ravid asemp ersus pendi ssene cipsu msedm agnal uctus
cursu snull amacc umsan fauci busan tesus pendi sselo borti sodio utmol estie
nonum myurn afeli svive rraju stoeu bland itfel islor emetl oremp hasel lusvo
lutpa ttort ornul lasem perri susat scele risqu efrin gilla nulla metus matti
sdolo rvita etemp usqua murna inqua msusp endis seveh icula diams itame tpulv
inarc ommod onisl liber otemp usorc iapel lente squet ellus enimv itaej ustov
ivamu sdiam pelle ntesq uequa mdone cnonl ibero quiso dioin terdu meuis moddo
necne cligu lasit ametd olora ccums ancom modon uncse mviva musgr avida duisi
tamet ipsum

It will look even more professional if you change the output to uppercase:

 ./affine.pl ipsum.txt | ./group -g -u
 
LOREM IPSUM DOLOR SITAM ETCON SECTE TUERA DIPIS CINGE LITDO NECPO SUERE ELITA
CFELI SMAEC ENASI PSUMV ESTIB ULUMD APIBU SLAOR EETFE LISDO NECFA CILIS ISMOL
LISQU AMINS OLLIC ITUDI NARCU ETLEO INHAC HABIT ASSEP LATEA DICTU MSTAE NEANV
ELDOL ORBLA NDITL OREMG RAVID ASEMP ERSUS PENDI SSENE CIPSU MSEDM AGNAL UCTUS
CURSU SNULL AMACC UMSAN FAUCI BUSAN TESUS PENDI SSELO BORTI SODIO UTMOL ESTIE
NONUM MYURN AFELI SVIVE RRAJU STOEU BLAND ITFEL ISLOR EMETL OREMP HASEL LUSVO
LUTPA TTORT ORNUL LASEM PERRI SUSAT SCELE RISQU EFRIN GILLA NULLA METUS MATTI
SDOLO RVITA ETEMP USQUA MURNA INQUA MSUSP ENDIS SEVEH ICULA DIAMS ITAME TPULV
INARC OMMOD ONISL LIBER OTEMP USORC IAPEL LENTE SQUET ELLUS ENIMV ITAEJ USTOV
IVAMU SDIAM PELLE NTESQ UEQUA MDONE CNONL IBERO QUISO DIOIN TERDU MEUIS MODDO
NECNE CLIGU LASIT AMETD OLORA CCUMS ANCOM MODON UNCSE MVIVA MUSGR AVIDA DUISI
TAMET IPSUM

Change the width of the group with the -w option. Let us make 6-char-long groups:

./affine.pl ipsum.txt | ./group -g -u -w 6

LOREMI PSUMDO LORSIT AMETCO NSECTE TUERAD IPISCI NGELIT DONECP OSUERE ELITAC
FELISM AECENA SIPSUM VESTIB ULUMDA PIBUSL AOREET FELISD ONECFA CILISI SMOLLI
SQUAMI NSOLLI CITUDI NARCUE TLEOIN HACHAB ITASSE PLATEA DICTUM STAENE ANVELD
OLORBL ANDITL OREMGR AVIDAS EMPERS USPEND ISSENE CIPSUM SEDMAG NALUCT USCURS
USNULL AMACCU MSANFA UCIBUS ANTESU SPENDI SSELOB ORTISO DIOUTM OLESTI ENONUM
MYURNA FELISV IVERRA JUSTOE UBLAND ITFELI SLOREM ETLORE MPHASE LLUSVO LUTPAT
TORTOR NULLAS EMPERR ISUSAT SCELER ISQUEF RINGIL LANULL AMETUS MATTIS DOLORV
ITAETE MPUSQU AMURNA INQUAM SUSPEN DISSEV EHICUL ADIAMS ITAMET PULVIN ARCOMM
ODONIS LLIBER OTEMPU SORCIA PELLEN TESQUE TELLUS ENIMVI TAEJUS TOVIVA MUSDIA
MPELLE NTESQU EQUAMD ONECNO NLIBER OQUISO DIOINT ERDUME UISMOD DONECN ECLIGU
LASITA METDOL ORACCU MSANCO MMODON UNCSEM VIVAMU SGRAVI DADUIS ITAMET IPSUM

3. Reversing the output (-r)

To run the output from end to beginning, use the -r option:

./affine.pl ipsum.txt | ./group -g -u -w 6 -r

MUSPIT EMATIS IUDADI VARGSU MAVIVM ESCNUN ODOMMO CNASMU CCAROL ODTEMA TISALU
GILCEN CENODD OMSIUE MUDRET NIOIDO SIUQOR EBILNO NCENOD MAUQEU QSETNE LLEPMA
IDSUMA VIVOTS UJEATI VMINES ULLETE UQSETN ELLEPA ICROSU PMETOR EBILLS INODOM
MOCRAN IVLUPT EMATIS MAIDAL UCIHEV ESSIDN EPSUSM AUQNIA NRUMAU QSUPME TEATIV
ROLODS ITTAMS UTEMAL LUNALL IGNIRF EUQSIR ELECST ASUSIR REPMES ALLUNR OTROTT
APTULO VSULLE SAHPME ROLTEM EROLSI LEFTID NALBUE OTSUJA RREVIV SILEFA NRUYMM
UNONEI TSELOM TUOIDO SITROB OLESSI DNEPSU SETNAS UBICUA FNASMU CCAMAL LUNSUS
RUCSUT CULANG AMDESM USPICE NESSID NEPSUS REPMES ADIVAR GMEROL TIDNAL BROLOD
LEVNAE NEATSM UTCIDA ETALPE SSATIB AHCAHN IOELTE UCRANI DUTICI LLOSNI MAUQSI
LLOMSI SILICA FCENOD SILEFT EEROAL SUBIPA DMULUB ITSEVM USPISA NECEAM SILEFC
ATILEE REUSOP CENODT ILEGNI CSIPID AREUTE TCESNO CTEMAT ISROLO DMUSPI MEROL

4. Bitwise complement (-b)

This is here just for the fun of it. Direct the output to a file as your terminal screen may go wild ;-)

./affine.pl ipsum.txt | ./group -g -u -w 6 -b > ipsumb

... finally, read the file "ipsumb" with your favourite screen pager or editor.

5. Final example

OK, in the previous examples, the affine.pl did not actually do anything. So, here is a real cipher example with the above Lorem Ipsum passage:

./affine.pl -r 3 -m 7 -z ipsum.txt | ./group -g -u
 
AIQUW VBJYW IAIQ JVCTW UCFIP JUFCU CYUQT VBVJ FVPGU AVCI PUFBI JYUQU UAVCT
FNUAV JWTUF UPTJV BJYWR UJCVM YAYW TBVMY JATIQ UUCNU AVJI PUFNT FVAVJ VJWIA
AVJXY TWVPJ IAAVF VCYV PTQFY UCAUI VPTF TMVC TJJUB ATCUT VFCY WJCTU PUTPR
UAIA IQMAT PVCA IQUWG QTRV TJUWB UQJYJ BUPV JJUPU FVBJY WJUW TGPTA YFCYJ
FYQJY JPYAA TWTFF YWJTP NTYFV MYJTP CUJYJ BUPV JJUAI MIQCV JIVI YCWIA UJCVU
PIPYW WZYQP TNUAV JRVRU QQTOY JCIUY MATP VCNUA VJAIQ UWUCA IQUWB TJUA AYJRI
AYCBT CCIQC IQPYA ATJUW BUQQV JYJTC JFUAU QVJXY UNQVP GVAAT PYAAT WUCYJ WTCCV
JIAI QRVCT UCUWB YJXYT WYQPT VPXYT WJYJB UPVJ JURU VFYAT VTWJ VCTWU CBYAR
VPTQF IWWI IPVJA AVMUQ ICUWB YJIQF VTBUA AUPCU JXYUC UAAYJ UPVWR VCTUO YJCIR
VRTWY JVTW BUAAU PCUJX YUXYT WIPU FPIPA VMUQI XYVJI VIVP CUQY WUYVJ WII
PUFPU FAVGY ATJVC TWUC IAIQT FFYWJ TPFIW WIIP YPFJU WRVRT WYJGQ TRVT YVJV
CTWUC VBJYW