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
File Name: APPLE_FILE.txt
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
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.
Once you download the file you need to follow below steps to use it.
Steps to do mass keyword replacement
Create two directories “source” and “destination” and place the shell script in parent of source and destination. As below screen shot shows
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.
Here is screen shot of sample file filename.txt in above example
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.
Here is screen shot of sample file filedata.txt in above example
Copy all the files where the keywords need to be replaced in the directory “source”.
Now provide the execute permission to the shell script by running below command
chmod +x replace_keywords.sh
Now the script is ready to be run, so execute below command
replace_keywords.sh filename.txt filedata.txt
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 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
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
Hope you find this post useful, please do let me know your comments, feedback.