, 2 min read
Printing COBOL Section Names
1. Problem statement. Print COBOL section names, ideally title-cased. Use a Perl one-liner for this task.
perl -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 Configuration
2 Input-Output
3 File
4 Working-Storage
5 A000-Steuer
6 A050-Init
7 V000-Verarbeitung
8 V100-Lein-Lesen
9 V900-Schlussarbeit
10 V200-Tabellen
11 V210-Loesche-Duplikate
12 V212-Open-Duplikate
13 V213-Fetch-Duplikat
14 V214-Close-Duplikate
15 V300-Delete-Vertrag
16 D710-Commit
17 T900-Sqlcode-Check
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":
- lower case everything
- 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.
perl -h
Usage: perl [switches] [--] [programfile] [arguments]
-0[octal/hexadecimal] specify record separator (\0, if no argument)
-a autosplit mode with -n or -p (splits $_ into @F)
-C[number/list] enables the listed Unicode features
-c check syntax only (runs BEGIN and CHECK blocks)
-d[t][:MOD] run program under debugger or module Devel::MOD
-D[number/letters] set debugging flags (argument is a bit mask or alphabets)
-e commandline one line of program (several -e's allowed, omit programfile)
-E commandline like -e, but enables all optional features
-f don't do $sitelib/sitecustomize.pl at startup
-F/pattern/ split() pattern for -a switch (//'s are optional)
-i[extension] edit <> files in place (makes backup if extension supplied)
-Idirectory specify @INC/#include directory (several -I's allowed)
-l[octnum] enable line ending processing, specifies line terminator
-[mM][-]module execute "use/no module..." before executing program
-n assume "while (<>) { ... }" loop around program
-p assume loop like -n but print line also, like sed
-s enable rudimentary parsing for switches after programfile
-S look for programfile using PATH environment variable
-t enable tainting warnings
-T enable tainting checks
-u dump core after parsing program
-U allow unsafe operations
-v print version, patchlevel and license
-V[:configvar] print configuration summary (or a single Config.pm variable)
-w enable many useful warnings
-W enable all warnings
-x[directory] ignore text before #!perl line (optionally cd to directory)
-X disable all warnings
Run 'perldoc perl' for more help with Perl.