JCL to reverse the file records
JCL Trick to write the output file in reverse order of input using SORT. If the input file has some records in Random order ( not in sorted order) and if the out file needs to be written exactly in reverse order. This can be achieved by using the SORT Overlay along with OUTREC BUild. Below id the JOB to do that.
//JOBSRT1 JOB MSGLEVEL=(1,1),MSGCLASS=X //****************************************************************** //* REVERESE FILE CONTENTS BY ADDING SEQ. NUM //*************************************************************** //STEP01 EXEC PGM=SORT //SYSPRINT DD SYSOUT=A //SYSOUT DD SYSOUT=* //SORTIN DD DSN=MAHYG.TEST.RNDM.FILE,DISP=SHR //SORTOUT DD DSN=MAHYG.TCEST.RVRSE.FILE,DISP=(NEW,CATLG,DELETE), // UNIT=SYSDA,SPACE=(CYL,(5,5)), // DCB=(RECFM=FB,LRECL=80,BLKSIZE=8000) //* //SYSIN DD * INREC OVERLAY=(40:SEQNUM,3,ZD) SORT FIELDS=(40,3,CH,D) OUTREC BUILD=(1,35) /*
Explanation:
- INREC OVERLAY statement adds a sequence in 40th position.
- SORT FILEDS statement sorts the records in descending order based on the newly added sequence number.
- OUTREC BUILD – As the sequence number is not required in output, this build statement copied the actual data till 35th position.
Input file has records as below and it has data till 35th position.
----+----1----+----2----+----3----+----4----+ 2RAGHAV RAJ PUNE LEAD 1MAHENDER REDDY HYDERABAD DEVELOPER 4VISHU CHENNAI MANAGER 3SHREYANSHREDDY MUMBAI ARCHITECT
Output after Reversing the order
----+----1----+----2----+----3----+----4 3SHREYANSHREDDY MUMBAI ARCHITECT 4VISHU CHENNAI MANAGER 1MAHENDER REDDY HYDERABAD DEVELOPER 2RAGHAV RAJ PUNE LEAD
But how to reverse the records if there is no free space available in the input file or output file. In this case, SORT job requires a temporary data set to add the sequence number and sort then copying this temporary data set into the required output file.
The same reversing of the file records using SORT can be achieved with below SORT card as well.
//SYSIN DD *
SORT FIELDS=(40,3,ZD,D)
INREC FIELDS=(1:1,35,40:SEQNUM,3,ZD,START=1,INCR=1)
OUTREC FIELDS=(1:1,35)
/*