Printing COBOL Section Names

· klm's blog


Original post is here: eklausmeier.goip.de

1. Problem statement. Print COBOL section names, ideally title-cased. Use a Perl one-liner for this task.

1perl -ne 'printf("%3d %s\n",++$i,join("-",map{ucfirst lc $_}split(/\-/,$1))) if /([\-\w]+)\sSECTION\./i' COBOL-Source.cbl

It will print "Working-Storage" as well, as this section is, well, a section.

A typical output might look like this:

 1  1 Configuration
 2  2 Input-Output
 3  3 File
 4  4 Working-Storage
 5  5 A000-Steuer
 6  6 A050-Init
 7  7 V000-Verarbeitung
 8  8 V100-Lein-Lesen
 9  9 V900-Schlussarbeit
10 10 V200-Tabellen
11 11 V210-Loesche-Duplikate
12 12 V212-Open-Duplikate
13 13 V213-Fetch-Duplikat
14 14 V214-Close-Duplikate
15 15 V300-Delete-Vertrag
16 16 D710-Commit
17 17 T900-Sqlcode-Check
18 18 T910-Sqlcode-Ftext

2. How it works. The if statement greps for the sections. In $1 we then have the section name. This often contains words separated by hyphens, e.g., A100-Initialization. The section name is then split at the hyphens using Perl's split() function.

Each substring is then "title-cased":

  1. lower case everything
  2. upper case first character with ucfirst

Finally, everything is concatenated with join.

3. Perl command-line options. Perl has below command-line options, which come handy for one-liners.

 1perl -h
 2
 3Usage: perl [switches] [--] [programfile] [arguments]
 4  -0[octal/hexadecimal] specify record separator (\0, if no argument)
 5  -a                    autosplit mode with -n or -p (splits $_ into @F)
 6  -C[number/list]       enables the listed Unicode features
 7  -c                    check syntax only (runs BEGIN and CHECK blocks)
 8  -d[t][:MOD]           run program under debugger or module Devel::MOD
 9  -D[number/letters]    set debugging flags (argument is a bit mask or alphabets)
10  -e commandline        one line of program (several -e's allowed, omit programfile)
11  -E commandline        like -e, but enables all optional features
12  -f                    don't do $sitelib/sitecustomize.pl at startup
13  -F/pattern/           split() pattern for -a switch (//'s are optional)
14  -i[extension]         edit <> files in place (makes backup if extension supplied)
15  -Idirectory           specify @INC/#include directory (several -I's allowed)
16  -l[octnum]            enable line ending processing, specifies line terminator
17  -[mM][-]module        execute "use/no module..." before executing program
18  -n                    assume "while (<>) { ... }" loop around program
19  -p                    assume loop like -n but print line also, like sed
20  -s                    enable rudimentary parsing for switches after programfile
21  -S                    look for programfile using PATH environment variable
22  -t                    enable tainting warnings
23  -T                    enable tainting checks
24  -u                    dump core after parsing program
25  -U                    allow unsafe operations
26  -v                    print version, patchlevel and license
27  -V[:configvar]        print configuration summary (or a single Config.pm variable)
28  -w                    enable many useful warnings
29  -W                    enable all warnings
30  -x[directory]         ignore text before #!perl line (optionally cd to directory)
31  -X                    disable all warnings
32
33Run 'perldoc perl' for more help with Perl.