Oh great, how did you do this, what language?

Oh great, how did you do this, what language?

A good friend of mine had a question to me today. He had a simple text file with blocks of various number of lines that he would like to be sorted by a string identifiable in each blocks header line.
The file itself was about 22,000 lines, to much for manual sorting. He asked me if I would have an idea for a tool or software like Textpad to do this job.
I just told him I can make this happen in an hour and then he can check results and if it fulfils his requirements. We agreed to this.
After just 15 minutes I sent the file back to him. He tested quickly and figured out it still has some curious things like being shown before . This was just a forgotten UPPER vs. lower case issue. Fixed and sent back a new file in another minute. File looked great then.

While I am writing the friend is keen to get the information on how I did this. He is a real good programmer; he has all the tools I am having. But sometimes it is really beneficial to think about the good old tools that you have somewhere around. Yes, we have text files, but this must not require modern tools like Textpad to handle such files efficiently.

I used a simple 120 lines COBOL code and a small JCL to run the job. 105 of the lines were just COPY & PASTE and the rest was quickly typed in. The JCL was copied and only the file names had to be added. With some tricks we probably can write the same COBOL code in less then 50 lines. It would still do the same damned good job but it would not look that easy it is looking right now.


The CODE:
###############################################################################
###############################################################################
###############################################################################
      ******************************************************************
      *                                                                *
      *    I D E N T I F I C A T I O N                     DIVISON     *
      *                                                                *
      ******************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID.    TstSRT04.
      ******************************************************************
      *                                                                *
      *    E N V I R O N M E N T                           DIVISON     *
      *                                                                *
      ******************************************************************
       ENVIRONMENT DIVISION.
      ******************************************************************
      *    C O N F I G U R A T I O N                       SECTION     *
      ******************************************************************
       CONFIGURATION SECTION.
       SPECIAL-NAMES.
           DECIMAL-POINT     is   COMMA.
      /*****************************************************************
      *    I N P U T - O U T P U T                         SECTION     *
      ******************************************************************
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT                     I1
               ASSIGN         TO  I1
               ORGANIZATION   IS  SEQUENTIAL SARF
               FILE STATUS    IS  STAT.
       SELECT                     O1
               ASSIGN         TO  O1
               ORGANIZATION   IS  SEQUENTIAL SARF
               FILE STATUS    IS  STAT.
       SELECT                     S1
               ASSIGN         TO  S1
               FILE STATUS    IS  STAT.
      /
      ******************************************************************
      *                                                                *
      *    D A T A                                         DIVISON     *
      *                                                                *
      ******************************************************************
       DATA DIVISION.
 
      ******************************************************************
      *    F I L E                                         SECTION     *
      ******************************************************************
       FILE SECTION.
       FD  I1.
       01  I1Rec                       PIC  X(500).
       FD  O1.
       01  O1Rec                       PIC  X(500).
       SD  S1.
       01  S1Rec.
           05  S1Key.
            10 S1KeyID                 PIC  X(100).
            10 S1KeyCnt                PIC  9(010).
           05  S1Data                  PIC  X(500).
 
      ******************************************************************
      *    W O R K I N G - S T O R A G E                   SECTION     *
      ******************************************************************
       WORKING-STORAGE SECTION.
       01          WorkFlds.
        05         wSrtKeyID           PIC  X(100).
        05         wSrtKeyCnt          PIC  9(010).
        05         tLgI                PIC  9(004)  COMP-5.
      ******************************************************************
      *            global workfields used anywhere in the pgm          *
      ******************************************************************
           05      STAT                PIC  X(002).
      ******************************************************************
      *                                                                *
      *    P R O C E D U R E                               DIVISON     *
      *                                                                *
      ******************************************************************
       PROCEDURE DIVISION.
      ******************************************************************
      *    A - M a i n                                     SECTION     *
      ******************************************************************
       A-Main SECTION.
           PERFORM  C-Sort1
           STOP   RUN
           .
       C-Sort1 SECTION.
           OPEN  INPUT   I1
           OPEN  OUTPUT  O1
           MOVE  LOW-VALUE  TO  wSrtKeyID
           MOVE  ZERO       TO  wSrtKeyCnt
           SORT  S1  ascending key  S1Key
                     INPUT  PROCEDURE IPROC
                     OUTPUT PROCEDURE OPROC
           CLOSE  I1
           CLOSE  O1
           .
       IPROC SECTION.
       IPROC-Loop.
           READ  I1  next
                 at  end  EXIT  SECTION
           end-read
           IF  I1Rec(01:17)  =  "/****** Object:  "
               MOVE  I1Rec(18:)     TO  wSrtKeyID
               MOVE  ZERO           TO  wSrtKeyCnt
               MOVE  length of          wSrtKeyID  TO  tLgI
               CALL  "YUCASE01"  using  wSrtKeyID, tLgI
           end-if
           ADD     1           TO  wSrtKeyCnt
           MOVE    wSrtKeyID   TO  S1KeyId
           MOVE    wSrtKeyCnt  TO  S1KeyCnt
           MOVE    I1Rec       TO  S1Data
           RELEASE S1Rec
           GO      IPROC-Loop
           .
       OPROC SECTION.
       OPROC-Loop.
           RETURN  S1
                 at  end  EXIT  SECTION
           end-return
           MOVE   S1Data  TO  O1Rec
           WRITE  O1Rec
           GO     OPROC-Loop
           .
      *-eof-*
###############################################################################
###############################################################################
###############################################################################

THE JOB CONTROL:
###############################################################################
###############################################################################
###############################################################################

stp  TSTSRT04;
asg  I1  C:\TEMP\SPS_TABLES_082311_NOTSORTED.TXT;
asg  O1  C:\TEMP\SPS_TABLES_082311_SORTED.TXT;
def  O1  StripSpace=Yes;
endstep;

done:
*eof
###############################################################################
###############################################################################
###############################################################################

The JOB EXECUTION:
###############################################################################
###############################################################################
###############################################################################


###############################################################################
###############################################################################
###############################################################################

Job ran in less then a second to sort all the blocks in the 22,000 lines.

COBOL can help you always. Just have the right ideas. And of course, you should have these at the right times. 🙂