Find/replace in DFSORT means searching for a specific string or value in the record and replacing it with another. You do this with FINDREP, specifying IN= (what to find) and OUT= (what to replace it with). FINDREP is used with INREC or OUTREC. You can list multiple find/replace pairs so that several different strings or values are replaced in one pass. When you use FINDREP in INREC, the sort and INCLUDE/OMIT see the record after replacement; when you use it in OUTREC, only the final output is changed. This page covers FINDREP syntax, single and multiple replacements, and when to use INREC vs OUTREC.
FINDREP lets you change specific content in each record without writing a program. You say "find this string (or value) and replace it with that one." The product scans the record (or the reformatted record when FINDREP is combined with BUILD/FIELDS) and substitutes every occurrence of the find value with the replace value. That is useful for normalizing codes (e.g. replace "TML" with "TPT"), fixing known bad values, or standardizing abbreviations. Unlike TRAN=LTOU, which converts all lowercase letters to uppercase, FINDREP works on exact strings or values—so you can replace "OLD" with "NEW" without affecting other characters.
The general form is:
12INREC FINDREP=(IN=find_value,OUT=replace_value) OUTREC FINDREP=(IN=find_value,OUT=replace_value)
For character strings you typically use C'...' (or the equivalent in your product) for IN and OUT. The find and replace values can be different lengths in some products (e.g. replace "XX" with "ABCD"); in others they must be the same length. Check your DFSORT manual. Example: replace the literal string "OLD" with "NEW" in every record:
12SORT FIELDS=COPY OUTREC FINDREP=(IN=C'OLD',OUT=C'NEW')
You can specify more than one replacement in the same FINDREP. List additional IN/OUT pairs after the first:
1OUTREC FINDREP=(IN=C'OLD1',OUT=C'NEW1',IN=C'OLD2',OUT=C'NEW2',IN=C'X',OUT=C'Y')
The product applies each pair. If the same substring could match more than one IN (e.g. "OLD1" and "OLD"), the order of application may matter: the first matching replacement might change the data so that a later IN no longer matches. Design your pairs so that the intended replacements are clear (e.g. longer strings first if one string is a prefix of another). Replacing every occurrence means that if "OLD" appears three times in the record, all three become "NEW" (unless your product supports a "replace first only" option).
Some DFSORT implementations allow you to find and replace numeric values—for example, replace the value 123 with 456. The exact syntax is product-dependent: you might specify IN=123, OUT=456, or you might need to give position, length, and format (e.g. PD, ZD) so the product knows where and how the number is stored. Replacing a numeric value is different from replacing the character string "123": the character form is three bytes EBCDIC "1" "2" "3", while a packed decimal 123 is typically two bytes. Check your manual for numeric FINDREP support and syntax.
INREC runs before the sort and before INCLUDE/OMIT. So FINDREP in INREC means that the record seen by the sort and by any conditions is already modified. Use INREC when you need to sort by the replaced value or filter on it—for example, replace a code in INREC and then SORT FIELDS= or INCLUDE COND= on that code. OUTREC runs after the sort. The sort and filter see the original record; only the output dataset has the replacement. Use OUTREC when you only need the change in the final file (e.g. writing a corrected extract) and the sort/filter should use the original data.
In some products you can use FINDREP together with BUILD or FIELDS in the same statement. For example, you might first build a reformatted record with OUTREC BUILD=(...) and then apply FINDREP to that built record. The exact syntax (e.g. BUILD=(...),FINDREP=(...) or FINDREP before BUILD) is product-dependent. This is useful when the replacement should apply only to the reformatted output (e.g. after you have moved or edited fields) rather than to the raw input. See your DFSORT documentation for the allowed combinations.
FINDREP replaces specific strings or values. TRAN=LTOU (or UTOL, ETOA, etc.) converts whole character sets (e.g. all lowercase to uppercase) in a field. ALTSEQ applies a byte-by-byte translation table. Use FINDREP when you want to change one exact string to another (e.g. "TML" → "TPT"). Use TRAN when you want case or code-page conversion. Use ALTSEQ when you need a custom one-byte-to-one-byte mapping.
Suppose a 2-byte status at position 45 can be "A ", "IA", or " I" and you want to standardize to "A ", "I ", and "I ". You could use multiple find/replace pairs (if the exact strings are unique in the record) or handle with IFTHEN/OVERLAY in BUILD. FINDREP is simplest when the strings to replace do not appear elsewhere:
1OUTREC FINDREP=(IN=C'IA',OUT=C'I ',IN=C' I',OUT=C'I ')
Order matters: if you had IN=C\' I\',OUT=C\'I \' first, then "IA" might be affected differently. Test with sample data to confirm the desired result.
Imagine you have a sentence and you want to change every "cat" to "dog". You don't change anything else—just find "cat" and write "dog" instead. FINDREP does that for the whole record: you say "find OLD, replace with NEW," and the computer does it. You can do several at once: find OLD1 replace NEW1, find OLD2 replace NEW2. We can do this before the sort (INREC) so the sort sees the new words, or only when writing the file (OUTREC) so the sort still saw the old words.
1. What is FINDREP in DFSORT?
2. How do you replace multiple different strings in one step?
3. When should you use FINDREP in INREC vs OUTREC?
4. Can FINDREP replace a numeric value (e.g. 123 with 456)?
5. Does FINDREP replace every occurrence of the string or only the first?