Sunday, September 22, 2019

How to reverse the file records through JCL

Write the output file in reverse order of input. 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.

//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=RATNESH.TEST.RNDM.FILE,DISP=SHR //SORTOUT DD DSN=RATNESH.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) /*



Input:

2RAGHAV RAJ PUNE LEAD 
1MAHENDER REDDY HYDERABAD DEVELOPER
4VISHU CHENNAI MANAGER 
3SHREYANSHREDDY MUMBAI ARCHITECT

Output:

3SHREYANSHREDDY MUMBAI ARCHITECT 
4VISHU CHENNAI MANAGER 
1MAHENDER REDDY HYDERABAD DEVELOPER 
2RAGHAV RAJ PUNE LEAD




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.

Note* 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.


No comments:

Post a Comment