coboljcl

Sorting in JCL or COBOL


I have an input file that goes like the following :

01 heading of data
inline-data of heading 01
inline-data of heading 01
09 heading of data
inline-data of heading 09
inline-data of heading 09
inline-data of heading 09
inline-data of heading 09
05  heading of data
inline-data of heading 05

Expected output :

01 heading of data
inline-data of heading 01
inline-data of heading 01
05  heading of data
inline-data of heading 05
09 heading of data
inline-data of heading 09
inline-data of heading 09
inline-data of heading 09
inline-data of heading 09

(although I am almost certain that there isn't because I searched), I'd like to know if there a way to do this in either JCL or the program ?

Thank you in advance.

SORT in JCL : sorted the headings separately from data,

Breaking point by program (if numeric, and if not, but cannot store the inline data beucase I don't know how many lines can there be for any given heading) : it requires reading the input file multiple times (hundreds of thousands of lines).


Solution

  • Look at the DFSORT smart trick "Sort groups of records" which can found be at

    https://www.ibm.com/support/pages/smart-dfsort-tricks

    In your case assuming that your input has LRECL=80 and RECFM=FB, you can use the following control cards

    //SYSIN    DD *                                   
      INREC IFTHEN=(WHEN=GROUP,                       
                   BEGIN=(04,08,CH,EQ,C'heading'),    
                    PUSH=(81:01,02))                  
                                                      
      SORT FIELDS=(81,02,CH,A),EQUALS                 
                                                      
      OUTREC BUILD=(01,80)                            
    /*                              
    
                  
    

    If your input is RECFM=V or VB then you can use the following control cards

    //SYSIN    DD *                                  
      INREC IFTHEN=(WHEN=INIT,                       
                   BUILD=(01,04,2X,5)),              
            IFTHEN=(WHEN=GROUP,                      
                   BEGIN=(10,08,CH,EQ,C'heading'),   
                    PUSH=(05:07,02))                 
                                                     
      SORT FIELDS=(05,02,CH,A),EQUALS                
                                                     
      OUTREC BUILD=(01,04,07)                        
    /*