Skip to main content

How can I batch edit a large number of programs?

  • February 15, 2013
  • 0 replies
  • 0 views

Problem:

A customer was required to change a single line in over 1000 programs to add text to the end of the PROGRAM-ID statement.   The format of the PROGRAM-ID is :

PROGRAM-ID.   MYPROGNAME.

The change required the format:

PROGRAM-ID  PROGRAMNAME IS INITIAL.

They did not wish to edit each file individually.

Resolution:

sed is made for this sort of job.  The answer is simple and very fast.  

#! /bin/sh

for FILE in *.cbl

do

echo $FILE

sed '/PROGRAM-ID/s/[.]$/ IS INITIAL./' $FILE > ./tmp/tmp.$FILE

diff $FILE ./tmp/tmp.$FILE >>./tmp/diff.txt

done

The script is placed in the directory containing the .cbl files.  I also create a tmp directory in the same directory.

When run, the script performs a loop reading every .cbl file.

It prints the filename to the screen.

Edits the line containing PROGRAM-ID to remove the trailing period from the program-name and adds IS INITIAL. to it.

Then it saves the file to the tmp directory with a prefix of tmp. added to the front.

It then runs a 'diff' and concatenates the output to diff.txt in the directory containing the modified sources.

The 1,030 files in my test were  a few pages long and took 12 seconds to complete the entire job.

I suggest you experiment with this sort of script. If you're happy, I would take the tmp prefix off so you don't have to change the filenames in tmp   i.e

sed '/PROGRAM-ID/s/[.]$/ IS INITIAL./' $FILE > ./tmp/$FILE

Always ensure you have secure copies of your sources when using sed.  For example, if you did not redirect the output to another directory, the above command will overwrite the original file.

Obviously, you can write your own script that best serves what you want to do.  sed is very powerful and is documented in many places.

Old KB# 4118