Mass keyword replacement across multiple text files using Shell script

Keyword replacement Heading
Mass Keyword replacement across multiple text files

Let’s consider a scenario where we have a  number of text files containing source code or data, which require some keyword replacement both for file content as well as file name.

 

To illustrate the requirement of keyword replacement  with a data example consider a sample file as shown below

Data Example

File Name: APPLE_FILE.txt

File Content:

INSERT INTO SBI_DEL_WB_PNBE VALUES(…);

INSERT INTO DEL_TAB VALUES(…);

l_tab_name := ‘PNBE_VALUES’;

Keyword Replacement requirement for File Name

APPLE => MANGO

Keyword Replacement requirement for File Content

SBI => PNB

DEL => MUM

WB => BR

PNBE => KlK

 

So after doing the keyword replacement as shown above below should be outcome

 

File Name: MANGO_FILE.txt

 

File Content:

INSERT INTO PNB_MUM_BR_KlK VALUES(…);

INSERT INTO MUM_TAB VALUES(…);

l_tab_name := ‘KlK_VALUES’;

 

This can be done manually for one or two files but imagine you have to do such updates to large number of files and the list of keywords  that need to be replaced is also long . Then it will turn out to be lot of manual work.

 

Here is a shell script that I have developed to automate this kind of task. It can be downloaded from github with below link.

Download Script from github

 

Once you download the file you need to follow below steps to use it.

Steps to do mass keyword replacement

Step 1

Create two directories “source” and “destination” and place the shell script in parent of source and destination. As below screen shot shows

File Location
File Location

Step 2

Create the list of old key words and new keyword to be replaced in file names and place in same directory where shell script is present.

<old_key_1>,<new_key_1>

<old_key_2>,<new_key_2>

…….

Here is screen shot of sample file filename.txt in above example

Filename Keywords
Filename keywords

Step 3

Create a list of old keywords and new keywords to be replaced in file data and place in same directory where shell script is present.

<old_key_1>,<new_key_1>

<old_key_2>,<new_key_2>

…….

Here is screen shot of sample file filedata.txt in above example

File Content Keywords
File Content Keywords

Step 4

Copy all the files where the keywords need to be replaced in the directory “source”.

Step 5

Now provide the execute permission to the shell script by running below command

chmod +x replace_keywords.sh

Step 6

Now the script is ready to be run, so execute below command

replace_keywords.sh filename.txt filedata.txt

Output

Once the command execution completes it creates files in “destination ” directory. Let’s check the file names and file content in destination directory.

File Name

File name got changed as per the keyword replacement specified in “filename.txt”. Here you can see that keyword APPLE got replaced with MANGO and GRAPE got replaced with BANANA. As below two screen shots show

source File Names
source File Names
destination File Names
destination File Names

File Content

File Content in destination directory got changed as per the keyword replacement specified in “filedata.txt”.  Here you can notice that SBI got replaced with PNB, WB with BR and PNBE with KlK as shown in below two screen shots

source file GRAPE_APPLE_commands.txt content
source file GRAPE_APPLE_commands.txt content
destination file BANANA_MANGO_command.txt content
destination file BANANA_MANGO_command.txt content

 

 

 

Hope you find this post useful, please do let me know your comments, feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *