Sync sort, Unpaired records of File1 have spaces for no records in F2 file. Can we replace those specific column's spaces by ZEROS?

StackOverflow https://stackoverflow.com/questions/20098504

  •  03-08-2022
  •  | 
  •  

Question

SORT:

 JOINKEYS FILES=F1,FIELDS=(5,4,A,10,20,A)                  
 JOINKEYS FILES=F2,FIELDS=(1,4,A,6,20,A)                   
 REFORMAT FIELDS=(F1:10,20,9,1,5,4,30,1,31,10,F2:27,10)    
 JOIN UNPAIRED,F1                                          
 INREC BUILD=(1,36,C',',37,10,C',',27,10,SFF,SUB,37,10,SFF,
                 EDIT=(TTTTTT))        

OUTPUT IS: *2nd row 4th column is spaces as unpaired from 2nd file, needs to be 0s automatically.

 22680372            ,5102,         1,         1,000000
 22222222            ,5105,         2,          ,000002   

OUTPUT shud be: *2nd row 4th column is 0 or 0000s as unpaired from 2nd file, needs to be 0s automatically.

 22680372            ,5102,         1,         1,000000
 22222222            ,5105,         2,         0,000002   
Was it helpful?

Solution

You need a condition, which means IFTHEN. You can't have IFTHEN and BUILD on the same INREC, but you can have multiple IFTHENs and BUILD can be part of an IFTHEN.

IFTHEN=(WHEN=INIT indicates something which should be done for every record (unconditional).

IFTHEN=(WHEN=(logical-expression will only be actioned if the condition is true.

Every BUILD statement makes a complete new intermediate record (intermediate between input and output). OVERLAY only affects the data at the position specified (assuming no extension of the record).

Your condition will be that the 46th byte of the record is space. You have already used SFF (did you try the other suggestions, especially FS?), so there is no need to make the value zero before the BUILD.

 JOINKEYS FILES=F1,FIELDS=(5,4,A,10,20,A)                  
 JOINKEYS FILES=F2,FIELDS=(1,4,A,6,20,A)                   
 REFORMAT FIELDS=(F1:10,20,9,1,5,4,30,1,31,10,F2:27,10)    
 JOIN UNPAIRED,F1                                          
 INREC IFTHEN=(WHEN=INIT,
                 BUILD=(1,36,
                        C',',
                        37,10,
                        C',',
                        27,10,SFF,
                         SUB,
                          37,10,SFF,
                         EDIT=(TTTTTT))),
       IFTHEN=(WHEN=(47,1,CH,EQ,C' '),
                OVERLAY=(46:C'0'))  

I don't format the statements like that just for fun, but to make them easier to understand and maintain.

OK, that solution was a little clunky. You can replace the INREC with this, which shows, for this type of data, an alternative to the EDIT:

INREC IFTHEN=(WHEN=INIT, 
                BUILD=(1,36, 
                       C',', 
                       37,10,FS,TO=FS,LENGTH=10,
                       C',', 
                       27,10,FS, 
                        SUB, 
                         37,10,FS, 
                        TO=FS,LENGTH=8)) 

This is much more natural, as the space gets turned into a zero with leading blanks with no conditions at all, and using references only to that field in its position on the REFORMAT record.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top