Reading COBOL data files with a copybook
A COBOL data file has no column headings, delimiters or line breaks. It is a continuous run of bytes in which every record has the same length, and the meaning of each byte comes from a copybook, which is a text file of data definitions written for the COBOL compiler. Without the copybook, the file cannot be interpreted. With it, you know that bytes 1 to 6 are an account number, bytes 7 to 26 are a name, and so on. TextPipe reads the copybook directly, so you do not have to calculate offsets by hand.
Real copybooks are not just a list of simple fields. A REDEFINES clause lets the same bytes be read in two ways. An OCCURS clause creates repeating groups, such as twelve monthly totals, and OCCURS DEPENDING ON makes the number of repeats vary from record to record. A single file may also hold several record types, for example a header, many detail records and a trailer, each with its own layout. TextPipe lets you choose the layout for each record type by testing a field value, and writes each type to its own output file or as separate sections.
Sample input and output
This copybook describes an order file with a repeating group:
01 ORDER-REC.
05 ORDER-NO PIC 9(5).
05 CUST-NAME PIC X(10).
05 LINE-COUNT PIC 9(2).
05 LINE-ITEM OCCURS 2 TIMES.
10 SKU PIC X(4).
10 QTY PIC 9(3).
After conversion from EBCDIC, one record of 31 characters looks like this:
00417SMITH CO 02AB12005CD34010
Written as CSV with one row per line item, so that the repeating group becomes rows that a database can store, the output is:
00417,SMITH CO,AB12,005
00417,SMITH CO,CD34,010
The order number and customer name are repeated on each row, which is the form that relational tables expect.
Typical settings
- Copybook file: load it, then check the calculated record length against the length reported by the mainframe job.
- Source encoding: EBCDIC for most mainframe files, ASCII for files produced by Micro Focus or other PC COBOL compilers.
- Record selection: pick the layout by the value of a record type field, and skip header and trailer records if they are not wanted.
- Output: CSV, tab-delimited, fixed width or XML, with one row per record or per repeating group entry.
Practical applications
Insurers and banks move policy and account history from mainframe systems into data warehouses. Government departments convert old payroll and tax files before a system is retired. Auditors read mainframe extracts directly, instead of waiting for a report to be written. Software vendors import customer data from legacy systems when a client switches to their product.
Common pitfalls
- A copybook that has been edited for a different program version may no longer match the data. Test on a small file and compare a few records against a printed report.
- Compiler options such as SYNC add padding bytes between binary fields. Make sure the same alignment is applied when reading.
- Signed numeric fields with SIGN SEPARATE or SIGN LEADING are stored differently from the default. Set the matching option.
Related pages
Numeric fields stored as packed decimal are covered on the COMP-3 packed data conversion page and in the packed decimal feature guide. For character conversion, see EBCDIC to ASCII. The banking and finance case study and the data conversion solutions page show complete projects.