Search and Replace
 Different Length Text Strings
When technology complements business    Copyright © 1987-2012  SimoTime Enterprises  All Rights Reserved
  Table of Contents Version 10.11.01 
  Introduction
  Call Interface
  The CMD Member
  The JCL Member
  The COBOL Demonstration Program
  The COBOL Search-and-Replace Routine
  The Copy File for the Pass Area
  Summary
  Software Agreement and Disclaimer
  Downloads and Links to Similar Pages
  Downloads and Links, Internet Access Required
  Downloads and Links, Local Access
  Glossary of Terms
  Comments, Suggestions or Feedback
  Company Overview
The SimoTime Home Page

Introduction
(Next) (Previous) (Table-of-Contents)

This suite of sample COBOL programs will show how to search and replace a text string value within a field with a text string value of a different length. This example contains two COBOL programs. The first is a demonstration program that creates fields with text, replacement arguments and replacement values. The second COBOL routine does the actual searching and replacement of the text string.

This may appear like a simple task that could be solved by a single line of code using the INSPECT statement as follows.

INSPECT field-name REPLACING FIRST 'abc' BY 'uvwxyz'
.

However, the preceding statement will generate an error since the INSPECT statement requires the search value and the replacement value to be of equal lengths.

Note: The COBOL language also has a REPLACING function that is used to replace text strings in a COBOL copy file at compile time. For more information about this function refer to the COPY REPLACING document available on the SimoTime web site. Refer to the Downloads and Links to Similar Pages section of this document.

             
Entry Point
ZOS or MFE
Entry Point
Windows
   
   
CBLREPJ1
jcl
CBLREPE1
cmd
Submit the Job
   
   
 
 
   
   
 
 
   
   
CBREPC1
cbl
   
   
CONSOLE
display
Replace string within Field
   
   
   
   
   
SIMOSUB1
cbl
Callable Search/Replace Routine
   
EOJ
End-Of-Job
 
Replacing a String within a Field

Note: The  light-green  boxes are unique to the Mainframe and Micro Focus Enterprise Server/Studio (or Micro Focus Mainframe Express). The  light-red  boxes are unique to the PC with Windows and Micro Focus. The  light-yellow  boxes are decision points or program transitions in the processing logic. The  light-blue  boxes identify data structures such as Files, VSAM Data Sets or Relational Tables. The  light-gray  boxes identify a system function or information item.

Call Interface
(Next) (Previous) (Table-of-Contents)

The following is the call statement that is used to call the COBOL routine that does the string substitution within a field.

CALL 'SIMOSUB1' USING SUBSTITUTE-PARAMETERS
.

The following are the fields that are passed from the demonstration program to the routine that does the actual string substitution within the field.

       01  SUBSTITUTE-PARAMETERS.
           05  SUB-BUFFER-SOURCE    pic X(128).
           05  SUB-BUFFER-TARGET    pic X(256).
           05  SUB-SEARCH-STRING    pic X(32).
           05  SUB-REPLACE-STRING   pic X(32).
           05  SUB-SEARCH-LENGTH    pic 9(2).
           05  SUB-REPLACE-LENGTH   pic 9(2).

The following describes each of the fields that are passed to the COBOL routine. The SUB-BUFFER-TARGET field is the only field that is modified by the search-and-replace routine (SIMOSUB1).

Parameter Description
SUB-BUFFER-SOURCE This field should contain the data with the possible text string to be changed.
SUB-BUFFER-TARGET The contents of this field is modified by the COBOL search-and-replace routine (SIMOSUB1). If a match is found from the search argument (SUB-SEARCH-STRING) it is replaced with the replacement value (SUB-REPLACE-STRING).
SUB-SEARCH-STRING This field is the search argument.
SUB-REPLACE-STRING This field is the replacement value.
SUB-SEARCH-LENGTH This field is the length of the search argument.
SUB-REPLACE-LENGTH This field is the length of the replacement value.
  A Description of the Fields in the Pass Area

The CMD Member
(Next) (Previous) (Table-of-Contents)

The following is the CMD member (CBLREPE1.CMD) used to run the job on a PC using Micro Focus Net Express Express.

@echo OFF
rem  * *******************************************************************
rem  *               CBLREPE1.CMD - a Windows Command File               *
rem  *         This program is provided by SimoTime Enterprises          *
rem  *           (C) Copyright 1987-2012 All Rights Reserved             *
rem  *             Web Site URL:   http://www.simotime.com               *
rem  *                   e-mail:   helpdesk@simotime.com                 *
rem  * *******************************************************************
rem  *
rem  * Text    - COBOL does string replacment for different length items
rem  * Author  - SimoTime Enterprises
rem  * Date    - January 01, 1987
rem  * Version - 04.08.20
rem  *
rem  * This set of programs illustrate the use a COBOL program to do
rem  * replacement of a text string with a text string of a different
rem  * length.
rem  *
rem  * This set of programs will run on a Personal Computer with Windows
rem  * and Micro Focus Net Express.
rem  *
rem  *   ************
rem  *   * CBLREPE1 *
rem  *   ********cmd*
rem  *        *
rem  *   ************     ************
rem  *   * SIMOEXEC ******* CONSOLE  *
rem  *   ********exe*     ************
rem  *        *  *
rem  *        *  *
rem  *        *  *        ************     ************
rem  *        *  ***call*** CBLREPC1 ******* CONSOLE  *
rem  *        *           ********cbl*     ************
rem  *        *                *
rem  *        *                *
rem  *        *           ************
rem  *        *           * SIMOSUB1 *
rem  *        *           ********cbl*
rem  *        *
rem  *   ************
rem  *   *   EOJ    *
rem  *   ************
rem  *
rem  * ********************************************************************
rem  * Step   1 of 2  Set the global environment variables...
rem  *
     call ..\Env1BASE
     if "%SYSLOG%" == "" set syslog=c:\SimoLIBR\LOGS\SimoTime.LOG
     set CmdName=CblRepE1
rem  *
     call SimoNOTE  "*******************************************************%CmdName%"
     call SimoNOTE  "Starting JobName %CmdName%"
rem  * ********************************************************************
rem  * Step   2 of 2  Execute the sample program...
rem  *
     run CblRepC1
rem  *
     call SimoNOTE  "Finished JobName %CmdName%, Job Status is %JobStatus%"
:End
     call SimoNOTE  "Conclude SysLog is %SYSLOG%"
     if not "%1" == "nopause" pause

The JCL Member
(Next) (Previous) (Table-of-Contents)

The following is the JCL member (CBLREPJ1.CMD) used to run the job on an IBM Mainframe with OS/390 or the PC using Micro Focus Mainframe Express.

//CBLREPJ1 JOB SIMOTIME,ACCOUNT,CLASS=1,MSGCLASS=A,NOTIFY=CSIP1
//* *******************************************************************
//*       This JCL Member is provided by: SimoTime Enterprises        *
//*           (C) Copyright 1987-2012 All Rights Reserved             *
//*             Web Site URL:   http://www.simotime.com               *
//*                   e-mail:   helpdesk@simotime.com                 *
//* *******************************************************************
//*
//* Text   - COBOL does string replacement for different length items
//* Author - SimoTime Enterprises
//* Date   - January 01, 1987
//*
//* This set of programs illustrate the use a COBOL program to do
//* replacement of a text string with a text string of a different
//* length.
//*
//* This set of programs will run on a mainframe under MVS or on
//* a Personal Computer running Windows and Mainframe Express by
//* Micro Focus.
//*
//*   ************
//*   * CBLREPJ1 *
//*   ********jcl*
//*        *
//*        *
//*   ************     ************
//*   * CBLREPC1 *-----* CONSOLE  *
//*   ********cbl*     ************
//*        *  *
//*        *  *
//*        *  *        ************
//*        *  ***call*** SIMOSUB1 *
//*        *           ********cbl*
//*        *
//*   ************
//*   *   EOJ    *
//*   ************
//*
//* *******************************************************************
//* Step   1 of 1  This is a single step job.
//*
//CBLREPS1 EXEC PGM=CBLREPC1
//STEPLIB  DD  DSN=SIMOTIME.DEMO.LOADLIB1,DISP=SHR
//SYSOUT   DD  SYSOUT=*
//*

The COBOL Demonstration Program
(Next) (Previous) (Table-of-Contents)

The following is the source listing (SIMOSUB1.CBL) for the COBOL demonstration program (SIMOSUB1.CBL).

       IDENTIFICATION DIVISION.
       PROGRAM-ID.    CBLREPC1.
       AUTHOR.        SIMOTIME ENTERPRISES.
      *****************************************************************
      * Copyright (C) 1987-2012 SimoTime Enterprises.                 *
      *                                                               *
      * All rights reserved.  Unpublished, all rights reserved under  *
      * copyright law and international treaty.  Use of a copyright   *
      * notice is precautionary only and does not imply publication   *
      * or disclosure.                                                *
      *                                                               *
      * Permission to use, copy, modify and distribute this software  *
      * for any commercial purpose requires a fee to be paid to       *
      * SimoTime Enterprises. Once the fee is received by SimoTime    *
      * the latest version of the software will be delivered and a    *
      * license will be granted for use within an enterprise,         *
      * provided the SimoTime copyright notice appear on all copies   *
      * of the software. The SimoTime name or Logo may not be used    *
      * in any advertising or publicity pertaining to the use of the  *
      * software without the written permission of SimoTime           *
      * Enterprises.                                                  *
      *                                                               *
      * Permission to use, copy and modify this software for any      *
      * non-commercial purpose and without fee is hereby granted,     *
      * provided the SimoTime copyright notice appear on all copies   *
      * of the software. The SimoTime name or Logo may not be used    *
      * in any advertising or publicity pertaining to the use of the  *
      * software without the written permission of SimoTime           *
      * Enterprises.                                                  *
      *                                                               *
      * SimoTime Enterprises makes no warranty or representations     *
      * about the suitability of the software for any purpose. It is  *
      * provided "AS IS" without any expressed or implied warranty,   *
      * including the implied warranties of merchantability, fitness  *
      * for a particular purpose and non-infringement. SimoTime       *
      * Enterprises shall not be liable for any direct, indirect,     *
      * special or consequential damages resulting from the loss of   *
      * use, data or projects, whether in an action of contract or    *
      * tort, arising out of or in connection with the use or         *
      * performance of this software                                  *
      *                                                               *
      * SimoTime Enterprises                                          *
      * 15 Carnoustie Drive                                           *
      * Novato, CA 94949-5849                                         *
      * 415.883.6565                                                  *
      *                                                               *
      * RESTRICTED RIGHTS LEGEND                                      *
      * Use, duplication, or disclosure by the Government is subject  *
      * to restrictions as set forth in subparagraph (c)(1)(ii) of    *
      * the Rights in Technical Data and Computer Software clause at  *
      * DFARS 52.227-7013 or subparagraphs (c)(1) and (2) of          *
      * Commercial  Computer Software - Restricted Rights  at 48      *
      * CFR 52.227-19, as applicable.  Contact SimoTime Enterprises,  *
      * 15 Carnoustie Drive, Novato, CA 94949-5849.                   *
      *                                                               *
      *****************************************************************
      *      This program is provided by SimoTime Enterprises         *
      *        Our e-mail address is: helpdesk@simotime.com           *
      *     Also, visit our Web Site at http://www.simotime.com       *
      *                                                               *
      *****************************************************************
      *
      *****************************************************************
      * Source Member: CBLREPC1.CBL
      * Copy Files:    PASSSUB1.CPY
      * Calls to:      SIMOSUB1
      *****************************************************************
      *
      * CBLREPC1 - String replacement of varying lengths.
      *
      * CALLING PROTOCOL
      * ----------------
      * Use standard procedure to RUN or ANIMATE.
      *
      * DESCRIPTION
      * -----------
      * This program provides an example of how to replace a text
      * string within a field with a text string of a different length.
      *
      *          ************
      *          * CBLREPJ1 *
      *          ********jcl*
      *               *
      *               *
      *          ************     ************
      *          * CBLREPC1 *-----* CONSOLE  *
      *          ********cbl*     ******dsply*
      *               *  *
      *               *  *
      *               *  *        ************
      *               *  ***call*** SIMOSUB1 *
      *               *           ********cbl*
      *               *
      *               *
      *          ************
      *          *   EOJ    *
      *          ************
      *
      *****************************************************************
      *
      * MAINTENANCE
      * -----------
      * 1987/05/22 Simmons, Created program.
      * 1994/04/17 Simmons, Updated for PC.
      *
      *****************************************************************
      *
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
      *****************************************************************
      *    Data-structure for Title and Copyright...
      *    ------------------------------------------------------------
       01  SIM-TITLE.
           05  T1 pic X(11) value '* CBLREPC1 '.
           05  T2 pic X(34) value 'Text String Replacement           '.
           05  T3 pic X(10) value ' v06.12.04'.
           05  T4 pic X(24) value ' http://www.simotime.com'.
       01  SIM-COPYRIGHT.
           05  C1 pic X(11) value '* CBLREPC1 '.
           05  C2 pic X(20) value 'Copyright 1987-2012 '.
           05  C3 pic X(28) value '--- SimoTime Enterprises ---'.
           05  C4 pic X(20) value ' All Rights Reserved'.

       01  SIM-THANKS-01.
           05  C1 pic X(11) value '* CBLREPC1 '.
           05  C2 pic X(32) value 'Thank you for using this softwar'.
           05  C3 pic X(32) value 'e provided from SimoTime Enterpr'.
           05  C4 pic X(04) value 'ises'.

       01  SIM-THANKS-02.
           05  C1 pic X(11) value '* CBLREPC1 '.
           05  C2 pic X(32) value 'Please send all inquires or sugg'.
           05  C3 pic X(32) value 'estions to the helpdesk@simotime'.
           05  C4 pic X(04) value '.com'.

      *****************************************************************
      * Message Buffer used by the Z-DISPLAY-MESSAGE-TEXT routine.    *
      *****************************************************************
       01  MESSAGE-BUFFER.
           05  MESSAGE-HEADER      pic X(11)   value '* CBLREPC1 '.
           05  MESSAGE-TEXT.
               10  MESSAGE-TEXT-1  pic X(68)   value SPACES.
               10  MESSAGE-TEXT-2  pic X(188)  value SPACES.

       COPY PASSSUB1.

      *****************************************************************
       PROCEDURE DIVISION.
           perform Z-POST-COPYRIGHT

           perform EXAMPLE-001

           perform EXAMPLE-002

           perform EXAMPLE-003

           perform EXAMPLE-004

           perform Z-THANK-YOU.

           GOBACK.

      *****************************************************************
      *    Example 1, Replace a text string with a text string
      *    of equal size.
       EXAMPLE-001.
           move '1-Before, replace >abc< with xyz'
             to SUB-BUFFER-SOURCE
           move 'abc' to SUB-SEARCH-STRING
           move 'xyz' to SUB-REPLACE-STRING
           add 3 to ZERO giving SUB-SEARCH-LENGTH
           add 3 to ZERO giving SUB-REPLACE-LENGTH
           call 'SIMOSUB1' using SUBSTITUTE-PARAMETERS
      *    Display the contents of the Source and Target Buffers.
           move SUB-BUFFER-SOURCE(1:71) to MESSAGE-TEXT
           perform Z-DISPLAY-MESSAGE-TEXT
           inspect SUB-BUFFER-TARGET
                   replacing first 'Before, ' by 'After,  '
           move SUB-BUFFER-TARGET(1:71) to MESSAGE-TEXT
           perform Z-DISPLAY-MESSAGE-TEXT
           exit.

      *****************************************************************
      *    Example 2, Replace a text string with a text string
      *    of greater size.
       EXAMPLE-002.
           move '2-Before, replace >abc< with uvwxyz'
             to SUB-BUFFER-SOURCE
           move 'abc' to SUB-SEARCH-STRING
           move 'uvwxyz' to SUB-REPLACE-STRING
           add 3 to ZERO giving SUB-SEARCH-LENGTH
           add 6 to ZERO giving SUB-REPLACE-LENGTH
           call 'SIMOSUB1' using SUBSTITUTE-PARAMETERS
      *    Display the contents of the Source and Target Buffers.
           move SUB-BUFFER-SOURCE(1:71) to MESSAGE-TEXT
           perform Z-DISPLAY-MESSAGE-TEXT
           inspect SUB-BUFFER-TARGET
                   replacing first 'Before, ' by 'After,  '
           move SUB-BUFFER-TARGET(1:71) to MESSAGE-TEXT
           perform Z-DISPLAY-MESSAGE-TEXT
           exit.

      *****************************************************************
      *    Example 3, Replace a text string with a text string
      *    of smaller size.
       EXAMPLE-003.
           move '3-Before, replace >abc< with M' to SUB-BUFFER-SOURCE
           move 'abc' to SUB-SEARCH-STRING
           move 'M' to SUB-REPLACE-STRING
           add 3 to ZERO giving SUB-SEARCH-LENGTH
           add 1 to ZERO giving SUB-REPLACE-LENGTH
           call 'SIMOSUB1' using SUBSTITUTE-PARAMETERS
      *    Display the contents of the Source and Target Buffers.
           move SUB-BUFFER-SOURCE(1:71) to MESSAGE-TEXT
           perform Z-DISPLAY-MESSAGE-TEXT
           inspect SUB-BUFFER-TARGET
                   replacing first 'Before, ' by 'After,  '
           move SUB-BUFFER-TARGET(1:71) to MESSAGE-TEXT
           perform Z-DISPLAY-MESSAGE-TEXT
           exit.

      *****************************************************************
      *    Example 4, Replace a text string with a text string
      *    of equal size.
       EXAMPLE-004.
           move 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' to SUB-BUFFER-SOURCE
           move 'DEF' to SUB-SEARCH-STRING
           move '123' to SUB-REPLACE-STRING
           add 3 to ZERO giving SUB-SEARCH-LENGTH
           add 3 to ZERO giving SUB-REPLACE-LENGTH
           call 'SIMOSUB1' using SUBSTITUTE-PARAMETERS
      *    Display the contents of the Source and Target Buffers.
           move SUB-BUFFER-SOURCE(1:71) to MESSAGE-TEXT
           perform Z-DISPLAY-MESSAGE-TEXT
           inspect SUB-BUFFER-TARGET
                   replacing first 'Before, ' by 'After,  '
           move SUB-BUFFER-TARGET(1:71) to MESSAGE-TEXT
           perform Z-DISPLAY-MESSAGE-TEXT
           exit.

      *****************************************************************
      * The following Z-Routines perform administrative tasks         *
      * for this program.                                             *
      *****************************************************************
       Z-POST-COPYRIGHT.
           move SIM-TITLE to MESSAGE-BUFFER
           perform Z-DISPLAY-MESSAGE-TEXT
           move SIM-COPYRIGHT to MESSAGE-BUFFER
           perform Z-DISPLAY-MESSAGE-TEXT
           exit.

      *****************************************************************
      * Display CONSOLE messages...                                   *
      *****************************************************************
       Z-DISPLAY-MESSAGE-TEXT.
           if  MESSAGE-TEXT-2 = SPACES
               display MESSAGE-BUFFER(1:79) upon console
           else
               display MESSAGE-BUFFER       upon console
           end-if
           move all SPACES to MESSAGE-TEXT
           exit.

      *****************************************************************
       Z-THANK-YOU.
           move SIM-THANKS-01 to MESSAGE-BUFFER
           perform Z-DISPLAY-MESSAGE-TEXT
           move SIM-THANKS-02 to MESSAGE-BUFFER
           perform Z-DISPLAY-MESSAGE-TEXT
           exit.

      *****************************************************************
      *      This example is provided by SimoTime Enterprises         *
      *        Our e-mail address is: helpdesk@simotime.com           *
      *     Also, visit our Web Site at http://www.simotime.com       *
      *****************************************************************

The COBOL Search-and-Replace Routine
(Next) (Previous) (Table-of-Contents)

The following is the source listing (CBLREPJ1.CMD) for the COBOL search and replace routine(SIMOSUB1.CBL). If the length of the search-text and replacement-text are equal then a simple INSPECT statement will perform the task, If the lengths are different then additional processing is required.

       IDENTIFICATION DIVISION.
       PROGRAM-ID.    SIMOSUB1.
       AUTHOR.        SIMOTIME ENTERPRISES.
      *****************************************************************
      * Copyright (C) 1987-2012 SimoTime Enterprises.                 *
      *                                                               *
      * All rights reserved.  Unpublished, all rights reserved under  *
      * copyright law and international treaty.  Use of a copyright   *
      * notice is precautionary only and does not imply publication   *
      * or disclosure.                                                *
      *                                                               *
      * Permission to use, copy, modify and distribute this software  *
      * for any commercial purpose requires a fee to be paid to       *
      * SimoTime Enterprises. Once the fee is received by SimoTime    *
      * the latest version of the software will be delivered and a    *
      * license will be granted for use within an enterprise,         *
      * provided the SimoTime copyright notice appear on all copies   *
      * of the software. The SimoTime name or Logo may not be used    *
      * in any advertising or publicity pertaining to the use of the  *
      * software without the written permission of SimoTime           *
      * Enterprises.                                                  *
      *                                                               *
      * Permission to use, copy and modify this software for any      *
      * non-commercial purpose and without fee is hereby granted,     *
      * provided the SimoTime copyright notice appear on all copies   *
      * of the software. The SimoTime name or Logo may not be used    *
      * in any advertising or publicity pertaining to the use of the  *
      * software without the written permission of SimoTime           *
      * Enterprises.                                                  *
      *                                                               *
      * SimoTime Enterprises makes no warranty or representations     *
      * about the suitability of the software for any purpose. It is  *
      * provided "AS IS" without any expressed or implied warranty,   *
      * including the implied warranties of merchantability, fitness  *
      * for a particular purpose and non-infringement. SimoTime       *
      * Enterprises shall not be liable for any direct, indirect,     *
      * special or consequential damages resulting from the loss of   *
      * use, data or projects, whether in an action of contract or    *
      * tort, arising out of or in connection with the use or         *
      * performance of this software                                  *
      *                                                               *
      * SimoTime Enterprises                                          *
      * 15 Carnoustie Drive                                           *
      * Novato, CA 94949-5849                                         *
      * 415.883.6565                                                  *
      *                                                               *
      * RESTRICTED RIGHTS LEGEND                                      *
      * Use, duplication, or disclosure by the Government is subject  *
      * to restrictions as set forth in subparagraph (c)(1)(ii) of    *
      * the Rights in Technical Data and Computer Software clause at  *
      * DFARS 52.227-7013 or subparagraphs (c)(1) and (2) of          *
      * Commercial  Computer Software - Restricted Rights  at 48      *
      * CFR 52.227-19, as applicable.  Contact SimoTime Enterprises,  *
      * 15 Carnoustie Drive, Novato, CA 94949-5849.                   *
      *                                                               *
      *****************************************************************
      *      This program is provided by SimoTime Enterprises         *
      *        Our e-mail address is: helpdesk@simotime.com           *
      *     Also, visit our Web Site at http://www.simotime.com       *
      *                                                               *
      *****************************************************************
      *
      *****************************************************************
      * Source Member: SIMOSUB1.CBL
      *****************************************************************
      *
      * SIMOSUB1 - String replacement of varying lengths.
      *
      * CALLING PROTOCOL
      * ----------------
      * Use standard procedure to RUN or ANIMATE.
      *
      * DESCRIPTION
      * -----------
      * This program provides an example of how to replace a text
      * string with is field with a text string of a different length.
      *
      *          ************
      *          * CBLREPJ1 *
      *          ********jcl*
      *               *
      *               *
      *          ************     ************
      *          * CBLREPC1 *-----* CONSOLE  *
      *          ********cbl*     ******dsply*
      *               *
      *               *
      *          ************
      *          * SIMOSUB1 *
      *          ********cbl*
      *
      *****************************************************************
      *
      * MAINTENANCE
      * -----------
      * 1987/05/22 Simmons, Created program.
      * 1994/04/17 Simmons, Updated for PC.
      *
      *****************************************************************
      *
       ENVIRONMENT DIVISION.
       DATA DIVISION.
      *****************************************************************
       WORKING-STORAGE SECTION.
       01  I-1            pic 9(5) value 0.
       01  I-2            pic 9(5) value 0.
       01  SUB-PTR        pic 9(5) value 0.

       01  SOURCE-SIZE    pic 9(5) value 0.
       01  TARGET-SIZE    pic 9(5) value 0.

      *****************************************************************
       LINKAGE SECTION.
       COPY PASSSUB1.

      *****************************************************************
       PROCEDURE DIVISION using SUBSTITUTE-PARAMETERS .

           if  SUB-SEARCH-LENGTH = SUB-REPLACE-LENGTH
               move SUB-BUFFER-SOURCE to SUB-BUFFER-TARGET
               inspect SUB-BUFFER-TARGET replacing
                       all SUB-SEARCH-STRING(1:SUB-SEARCH-LENGTH)
                        by SUB-REPLACE-STRING(1:SUB-REPLACE-LENGTH)
           else
               perform INSPECT-AND-REPLACE-EXTENDED
           end-if

           GOBACK.

      *****************************************************************
       INSPECT-AND-REPLACE-EXTENDED.
           add length of SUB-BUFFER-SOURCE to ZERO giving SOURCE-SIZE
           add length of SUB-BUFFER-TARGET to ZERO giving TARGET-SIZE
           move SPACES to SUB-BUFFER-TARGET
           subtract SUB-PTR from SUB-PTR
           inspect SUB-BUFFER-SOURCE
               tallying    SUB-PTR
               for CHARACTERS
               before initial SUB-SEARCH-STRING(1:SUB-SEARCH-LENGTH)
           if  SUB-PTR < SOURCE-SIZE - 1
               add  1 to SUB-PTR giving I-1
               add  1 to SUB-PTR giving I-2
               move SPACES to SUB-BUFFER-TARGET
               move SUB-BUFFER-SOURCE(1:SUB-PTR) to SUB-BUFFER-TARGET
               move SUB-REPLACE-STRING(1:SUB-REPLACE-LENGTH)
                 to SUB-BUFFER-TARGET(I-2:SUB-REPLACE-LENGTH)
               add  SUB-SEARCH-LENGTH to I-1
               add  SUB-REPLACE-LENGTH to I-2
               move SUB-BUFFER-SOURCE(I-1:SOURCE-SIZE - I-1)
                 to SUB-BUFFER-TARGET(I-2:TARGET-SIZE - I-2)
           else
               move SUB-BUFFER-SOURCE to SUB-BUFFER-TARGET
           end-if
           exit.
      *****************************************************************
      *      This example is provided by SimoTime Enterprises         *
      *        Our e-mail address is: helpdesk@simotime.com           *
      *     Also, visit our Web Site at http://www.simotime.com       *
      *****************************************************************

The Copy File for the Pass Area
(Next) (Previous) (Table-of-Contents)

The following is the source listing for the COBOL Copy File (PASSSUB1.CPY) used to call the search and replace routine

      *****************************************************************
      *     Data Structure or Pass Area used for calling SIMOSUB1.    *
      *****************************************************************
      *         Copyright (C) 1987-2012 SimoTime Enterprises          *
      *                     All Rights Reserved                       *
      *****************************************************************
      *              Provided by SimoTime Enterprises                 *
      *        Our e-mail address is: helpdesk@simotime.com           *
      *     Also, visit our Web Site at http://www.simotime.com       *
      *****************************************************************
      * SUB-BUFFER-SOURCE  - This field contains the text with data   *
      *                      to be replaced. This field is not        *
      *                      modified by the SimoSUB1 routine.        *
      * SUB-BUFFER-TARGET  - This field contains the text with data   *
      *                      that will be modified by the SimoSUB1    *
      *                      routine.                                 *
      * SUB-SEARCH-STRING  - This field contains the text string for  *
      *                      the search value.                        *
      * SUB-REPLACE-STRING - This field contains the text string for  *
      *                      the replace value.                       *
      * SUB-SEARCH-LENGTH  - This field defines the length of the     *
      *                      search string.                           *
      * SUB-REPLACE-LENGTH - This field defines the length of the     *
      *                      replace value.                           *
      *****************************************************************
       01  SUBSTITUTE-PARAMETERS.
           05  SUB-BUFFER-SOURCE    pic X(1024).
           05  SUB-BUFFER-TARGET    pic X(1024).
           05  SUB-SEARCH-STRING    pic X(128).
           05  SUB-REPLACE-STRING   pic X(128).
           05  SUB-SEARCH-LENGTH    pic 9(3).
           05  SUB-REPLACE-LENGTH   pic 9(3).
      ***  PASSSUB1 - End-of-Copy File - - - - - - - - - - - PASSSUB1 *
      *****************************************************************
      *

Summary
(Next) (Previous) (Table-of-Contents)

This suite of programs is provided as a COBOL programming example of one of the possible solutions to the problem of searching a data field and replacing a text string within the field with a text string of a different length.

This document may be used as a tutorial for new programmers or as a quick reference for experienced programmers. In the world of programming there are many ways to solve a problem. This document and the links to other documents are intended to provide a choice of alternatives.

Software Agreement and Disclaimer
(Next) (Previous) (Table-of-Contents)

Permission to use, copy, modify and distribute this software, documentation or training material for any purpose requires a fee to be paid to SimoTime Enterprises. Once the fee is received by SimoTime the latest version of the software, documentation or training material will be delivered and a license will be granted for use within an enterprise, provided the SimoTime copyright notice appear on all copies of the software. The SimoTime name or Logo may not be used in any advertising or publicity pertaining to the use of the software without the written permission of SimoTime Enterprises.

SimoTime Enterprises makes no warranty or representations about the suitability of the software, documentation or learning material for any purpose. It is provided "AS IS" without any expressed or implied warranty, including the implied warranties of merchantability, fitness for a particular purpose and non-infringement. SimoTime Enterprises shall not be liable for any direct, indirect, special or consequential damages resulting from the loss of use, data or projects, whether in an action of contract or tort, arising out of or in connection with the use or performance of this software, documentation or training material.

Downloads and Links to Similar Pages
(Next) (Previous) (Table-of-Contents)

This section includes links to documents with additional information that is beyond the scope and purpose of this document. The first sub-section requires an internet connection, the second sub-section references locally available documents.

Note:  A SimoTime License is required for the items to be made available on a local server.

Downloads and Links, Internet Access Required
(Next) (Previous) (Table-of-Contents)

The following links will require an internet connect.

A good place to start is The SimoTime Home Page for access to white papers, program examples and product information.

Download this evaluation package for the Search-and-Replace using a COBOL Program that will describe how to replace a text string in a field with a text string of a different size.

Explore The COBOL Connection in the SimoTime Library for more examples of COBOL programming techniques and sample code.

Explore The File Status Return Codes to interpret the results of accessing VSAM data sets and QSAM files.

Explore The Micro Focus Web Site for more information about products and services available from Micro Focus.

Downloads and Links, Local Access
(Next) (Previous) (Table-of-Contents)

The following links may be accessible without an internet connection.

View the document that describes and demonstrates the COPY REPLACING function of the COBOL compiler.

Explore The File Status Return Codes to interpret the results of accessing VSAM data sets and QSAM files.

Glossary of Terms
(Next) (Previous) (Table-of-Contents)

Check out  The SimoTime Glossary  for a list of terms and definitions used in the documents provided by SimoTime.

Comments, Suggestions or Feedback
(Next) (Previous) (Table-of-Contents)

This document was created and is maintained by SimoTime Enterprises.

If you have any questions, suggestions, comments or feedback please call or send an e-mail to: helpdesk@simotime.com

We appreciate hearing from you.

Company Overview
(Next) (Previous) (Table-of-Contents)

Founded in 1987, SimoTime Enterprises is a privately owned company. We specialize in the creation and deployment of business applications using new or existing technologies and services. We have a team of individuals that understand the broad range of technologies being used in today's environments. This includes the smallest thin client using the Internet and the very large mainframe systems. There is more to making the Internet work for your company's business than just having a nice looking WEB site. It is about combining the latest technologies and existing technologies with practical business experience. It's about the business of doing business and looking good in the process. Quite often, to reach larger markets or provide a higher level of service to existing customers it requires the newer Internet technologies to work in a complementary manner with existing corporate mainframe systems.

Whether you want to use the Internet to expand into new market segments or as a delivery vehicle for existing business functions simply give us a call or check the web site at http://www.simotime.com


Return-to-Top
Search and Replace, Different Length Text Strings
Copyright © 1987-2012 SimoTime Enterprises  All Rights Reserved
When technology complements business
http://www.simotime.com