How to use SORT to move blank lines to the end of

2019-08-23 10:44发布

问题:

I have 9787 records of which the first 17 lines are blank. I want to move those 17 lines to the end of the file. How can I do that?

回答1:

The below will sort the input dataset with the blank lines at the end of the SORTOUT DD

//SORT EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTWK01 DD SPACE=(CYL,(10,5),RLSE)
//SORTWK02 DD SPACE=(CYL,(10,5),RLSE)
//SORTWK03 DD SPACE=(CYL,(10,5),RLSE)
//SORTIN DD DSN=INPUT.DATASET,DISP=SHR
//SORTOUT DD SYSOUT=*
//SYSIN DD *
SORT FIELDS=(1,80,CH,D)
//*

FIELDS=(1,80,CH,D) means it is sorting in descending order from Position 1 for 80 characters using character data.
If you dataset is wider than 80 characters you might need to put the actual width here or the blank lines might not be put at the end.



回答2:

There is no need to Sort the data. Lucky that the data lines were 1) in order as per entire record, 2) in descending order :-).

This temporarily extends each record by adding a sequence number at the "end" of each record (five digits should allow for expansion). With OUTFIL OMIT, blank lines that are from the first 17 records of the data are dropped. With TRAILER1 and the "slash operator" "/", 17 blank lines are added to the "end" of the file. The REMOVECC is due to the need to not have the printer control-character that TRAILER1 (a reporting function) would otherwise add. The BUILD on OUTFIL is to return the record to its original size, dropping the 5-digit sequence number.

OPTION COPY 

INREC IFTHEN=(WHEN=INIT,OVERLAY=(81:SEQNUM,5,ZD))

OUTFIL OMIT=((1,80,CH,EQ,C' ', 
           AND,81,5,ZD,LE,17)), 
       BUILD=(1,80), 
       REMOVECC, 
       TRAILER1=(/,/,/,/,/,/,/,/,/,/,/,/,/,/,/,/)