Here describe the micro focus utility 'mfsort' usage, and give some samples:
Usage: mfsort . . . TAKE [CHAR-EBCDIC|SIGN-EBCDIC] SORT|MERGE FIELDS (start,length,type,A|D,...)|COPY RECORD F|V,min,max USE filename ORG LS|SQ|RL|IX RECORD F|V|FCxxx|FVxxx,min,max [KEY (start,length,P|A|AD|C,...)] GIVE (with parameters as for USE) INCLUDE|OMIT COND=(condition) [FORMAT=type] INREC FIELDS=(field-specifier...) OUTREC FIELDS=(field-specifier...) MODS {exit-name=(exit-routine)}... SUM FIELDS=(sum-field-specifier)|NONE [FORMAT=type] OUTFIL GIVE STARTREC start-record ENDREC end-record INCLUDE|OMIT SAVE SPLIT OUTREC LINES/HEADER1/TRAILER1/HEADER2/TRAILER2/SECTIONS/NODETAIL
There are 2 types of command line syntax:
Option 1, instructions are placed in command line directly.
Option 2: instructions are placed in a file, and use 'take' option set the file.
Notice: lines start with a star('*') in instruction_file are comment lines.
SORT/MERGE is the key instruction; SORT and MERGE are mutually exclusive.
Syntax:
SORT/MERGE FIELDS=(field_instruction,…)
field_instruction ::=
(<start>,<length>,{<type>},<A|D>,...){,FORMAT=<type>}
Instructions |
Description |
SORT FIELDS=(1,2,NU,D) |
sort field start from 1, and length is 2 |
SORT FIELDS=(1,2,NU,D,5,2,CH,A) |
sort 2 fields |
SORT FIELDS=(1,2,D),FORMAT=NU |
use FORMAT instruction to define format |
MERGE FIELDS=COPY |
COPY |
There are common used data types:
Data type |
COBOL data type |
CH |
PIC X USAGE DISPLAY |
NU |
PIC 9 USAGE DISPLAY |
PD |
PIC S9 COMP-3 |
BI |
USAGE COMP |
The USE Statement declares the input files, as well as record type, and record length, and the overall organization of the file.
Syntax:
USE <filename> ORG <LS|SQ|RL|IX> RECORD <F|V>,min,max [KEY …,]
The GIVE Statement declares the output files, as well as record type, and record length, and the overall organization of the file.
More than one GIVE instruction can be used when we need to output multiply output files, for example:
GIVE DATA.SORTOUT1
GIVE DATA.SORTOUT2 ORG LS
GIVE DATA.SORTOUT3 ORG SQ
GIVE DATA.SORTOUT4 ORG SQ RECORD F,80
GIVE DATA.SORTOUT5 ORG SQ RECORD V,1,80
Above commands will output 5 output files at the same time with same data content but different file format, or record format.
Syntax:
USE <filename> ORG <LS|SQ|RL|IX> RECORD <F|V>,min,max [KEY …,]
(This is same as USE instruction)
The INCLUDE/OMIT statement describes a sort filter with a portion of the input file, and a comparison expression. If the portion of the file tests true for the comparison, the record is included/excluded in the output file.
INCLUDE and OMIT are mutually exclusive; and only one INCLUDE or OMIT instruction can be used.
Syntax:
INCLUDE/OMIT COND=(condition) [FORMAT=type]
Condition::=
({start-pos,length,data-type,comparison-expression}...)
For example:
Instructions |
Description |
INCLUDE COND=(1,2,CH,EQ,C'11') |
|
INCLUDE COND=(1,2,CH,EQ,C'11') FORMAT=CH |
FORMAT has priority |
INCLUDE COND=(1,2,EQ,C'11') FORMAT=CH |
|
INCLUDE COND=(1,2,CH,EQ,C'11',OR,1,2,CH,EQ,C’22’) |
|
Comparison-operator
Instructions |
Description |
EQ |
Equal to |
NE |
Not Equal to |
GT |
Greater Than |
GE |
Greater Than or Equal |
LT |
Less Than |
LE |
Less Than or Equal |
INREC provides a mechanism for taking portions of an input file, and creating a new input file for the SORT function.
Only one INREC instruction can be used.
INREC will be applied after INCLUDE/OMIT instruction is finished.
Syntax:
INREC FIELDS=(field-specifier...)
OUTREC provides a mechanism for reformatting an output file, using portions of the output file that have been created by the SORT.
Only one OUTREC instruction can be used.
OUTREC will be applied after INCLUDE/OMIT and INREC instruction is finished.
Syntax:
OUTREC FIELDS=(field-specifier...)
If you wish to produce more than one output file, with different output and different formats, you can associate an outfil statement with a give statement, and then describe the reformatting associated with the OUTREC statement.
OUTFIL instruction will be applied after INCLUDE/OMIT/INREC/OUTREC instructions are finished.
Following are sub-instructions in OUTFIL instruction:
Description: a single output file
Syntax: GIVE <filename>
For example: OUTFIL give DATA.SORTOUT
Description: the first record number to be output;
Syntax: STARTREC=<num>
For example: OUTFIL give DATA.SORTOUT STARTREC=3
Description: the last record number to be output.
Syntax: STARTREC=<num>
For example: OUTFIL give DATA.SORTOUT STARTREC=5
Syntax: INCLUDE=(condition)
Description:
For example: OUTFIL give DATA.SORTOUT INCLUDE=(1,2,CH,EQ,C'BB')
Syntax: OMIT=(condition)
Description:
For example: OUTFIL give DATA.SORTOUT OMIT=(1,2,CH,EQ,C'BB')
Syntax: OUTREC=( field-specifier)
Description:
For example: OUTFIL give DATA.SORTOUT OUTREC=(1:3,2,3:1,2,5,8); OUTREC provides a mechanism for reformatting an output file, using portions of the output file that have been created by the SORT.
INCLUDE/OMIT -> INREC -> SORT -> OUTREC -> OUTFIL
OUTFIL: STARTREC&ENDREC > INCLUDE/OMIT> OUTREC
So INCLUDE/OMIT is firstly be applied when records are read from input; then INREC, SORT, OUTREC, and OUTFIL instruction will be applied one by one.
Inner OUTFIL instruction their sub-instructions are applied ordered also.
For example:
Support DATA.SORTIN has following records:
11CCcc11
22AAaa22
33BBbb33
44XXXX44
Support SORTIN.script has following instructions:
SORT FIELDS=(1,2,CH,A)
USE DATA.SORTIN ORG LS RECORD F,8
INREC FIELDS=(3,6)
OMIT COND=(1,2,CH,EQ,C'44')
OUTREC FIELDS=(3,4)
OUTFIL GIVE DATA.SORTOUT STARTREC=2 ENDREC=3 OMIT=(1,2,CH,EQ,C'bb') OUTREC=('bb',3,4)
Step 1: OMIT COND=(1,2,CH,EQ,C'44')
To omit record whose first 3 char is '44', so the 4th record is discarded.
Result:
11CCcc11
22AAaa22
33BBbb33
Step 2: INREC FIELDS=(3,6)
Reorganize the in-record, skip the first 2 chars:
Result:
CCcc11
AAaa22
BBbb33
Step 3: SORT FIELDS=(1,2,CH,A)
Ascend sort records based on first 2 chars.
Result:
AAaa22
BBbb33
CCcc11
Step 4: OUTREC FIELDS=(3,4)
Reorganize the out-record, skip the first 2 chars again.
Result:
aa22
bb33
cc11
Step 5: OUTFIL -> STARTREC=2 ENDREC=3
To get only the 2nd and 3rd records
Result:
bb33
cc11
Step 6: OUTFIL -> OMIT=(1,2,CH,EQ,C'bb')
To omit records the first 2 chars are 'bb'
Result:
cc11
Step 7: OUTFIL -> OUTREC=('bb',3,4)
Reorganize the record with first 2 chars are 'bb', and the 3rd, 4th char.
Result:
bb11
OUTREC provides a mechanism for reformatting an output file, using portions of the output file that have been created by the SORT.
Syntax:
OUTREC::=
OUTREC [=] ([[{format-char}]...){start-pos, length,} [change-nomatch]...)]]
change-nomatch::=
CHANGE=({change-length, scan-string, change-string}... ) {,} NOMATCH=(start-pos, length )
Suppose INREC FIELDS=(1,8), its value is
AABBCCDD
EEFFGGHH
Sample 1. OUTREC=(1,8)
Get the field from 1st byte, with 8 bytes length.
AABBCCDD
EEFFGGHH
Sample 2. OUTREC=(1,4)
Get the field from 1st byte, with 4 bytes length.
Result:
AABB
EEFF
Sample 3. OUTREC=(5,4)
Get the field from 5st byte, with 4 bytes length.
Result:
CCDD
GGHH
Sample 4. OUTREC=(5,2,3:1,2), or OUTREC=(1:5,2,3:1,2)
The first 2 chars in output are coming from 5th char in source, and 2 chars start from 3rd byte are coming from 1st byte.
Result:
CCAA
GGEE
Sample 5. OUTREC=('XX',1,2), or OUTREC=('XX',3:1,2), or OUTREC=('XX',1:1,2)
The first 2 chars are constant 'XX' in output, and the next 2 chars are coming from first 2 chars in source.
Result:
XXAA
XXEE
Sample 6. OUTREC=('XX',3,4)
The first 2 chars are constant 'XX', and the chars(start 3rd length 4) are coming from 3rd chars length 4 in source.
Result:
XXBBCC
XXFFGG
Sample 7. OUTREC=('XX',5,4)
The first 2 chars are constant 'XX', and the chars(start 3rd length 4) are coming from 5rd chars length 4 in source.
Result:
XXCCDD
XXGGHH
Sample 8. OUTREC=('XX',3:5,4)
The first 2 chars are constant 'XX', and the chars(start 3rd length 4) are coming from 5rd chars length 4 in source.
Result:
XXCCDD
XXGGHH
Sample 9. OUTREC=('XX',5:3,4)
The first 2 chars are constant 'XX', and the chars(start 5rd length 4) are coming from 3rd chars length 4 in source
The 3rd and 4th char in output are undefined, so whitespace is used.
Result:
XX BBCC
XX FFGG
Sample 10. OUTREC=('XX',3,4,CHANGE=(4,'BBCC','MMNN'),NOMATCH=(3,4))
Replace the chars (start from 3rd, length is 4) with 'MMNN' if they are 'BBCC', otherwise use the chars(start from 3rd, length 4) in source.
Result:
XXMMNN
XXFFGG
Sample 11. OUTREC=('XX',3,4,CHANGE=(4,'BBCC','MMNN'),NOMATCH=(5,4))
Replace the chars (start from 3rd, length is 4) with 'MMNN' if they are 'BBCC', otherwise use the chars(start from 5rd, length 4) in source.
Result:
XXMMNN
XXGGHH
Sample 12. OUTREC=('XX',3,4,CHANGE=(4,'BBCC','MM'),NOMATCH=(3,4))
Replace the chars (start from 3rd, length is 4) with 'MM' if they are 'BBCC', otherwise use the chars(start from 5rd, length 4) in source.
Result:
XXMM
XXFFGG
Sample 13. OUTREC=('XX',3,4,CHANGE=(4,'BBCC','MMNN'),NOMATCH=(C'YYYY'))
Replace the chars (start from 3rd, length is 4) with 'MMNN' if they are 'BBCC', otherwise use the constants chars 'YYYY'.
Result:
XXMMNN
XXYYYY
Sample 14. OUTREC=('XX',3,4,CHANGE=(4,'BBCC','MMNN','FFGG','OOPP'),NOMATCH=(3,4))
Replace the chars (start from 3rd, length is 4) with 'MMNN' if they are 'BBCC', and replace the chars with 'OOPP' if they are 'FFGG'.
Result:
XXMMNN
XXOOPP
Sample 15. OUTREC=('XX',3,4,CHANGE=(4,'BBCC','MMNNOO'),NOMATCH=(3,4))
Failure: Target literal 'MMNNOO' too long in CHANGE
Because length of 'MMNNOO' cannot larger than 4.