Batch Find and Replace in RTF _Case Sensitive

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

Batch Find and Replace in RTF _Case Sensitive

#1 Post by slavvo67 »

I've been playing around with this for what seems like forever so I decided to share my results.

Background: I have multiple RTF files. I want to find and replace based on the following criteria: (1) Case sensitive find/replace (2) Include phrases with spaces between words (3) Using a find variable and a replace variable for the FIND and REPLACE (4) Doing a batch through a particular directory searching multiple RTF files.

I've tried with Sed -e, Sed -i, M4 and different Awk attempts. I finally had success with the following:

Code: Select all

echo "Enter Variable to Change (i.e. FIRMNAME ) Do Not Use: &"
read var222

echo "Enter Replacement Words (i.e. Charlie Inc. ) Do Not Use: &"
read repl111

for i in *.rtf; do

	awk -v var="$var222" -v var2="$repl111" '{gsub ( var, var2 ) }; {print}' $i >/new/directory/"$i" 

done
I thought this might be worth sharing with others. Obviously, you can use this with .txt files, as well if you change the .rtf to .txt.

I hope someone can use this.

Best,

Slavvo67
Last edited by slavvo67 on Sun 07 Dec 2014, 17:42, edited 1 time in total.

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

The Script

#2 Post by slavvo67 »

#!/bin/bash


# The following was put together batch multiple RTF files in one directory.
#
# We are taking one word or phrase and changing it across all RTF's.
#
# Criteria: (1) Case Sensitive.
# (2) Accept single words or phrases with spaces.
# (3) Use Variables for FIND and REPLACE (not fixed coding).
# (4) Batch processing in a specific directory.
# (5) Specifically tested for RTF files.
# Note: Certain special characters cannot be used in the name change.
# For example, & passes the original name back to the result.
#
# We are using RTF files because author finds they cross convert nicely with MS product, Libre Office and ABI Word.
#
# Destination for this example is /root/new1. You can easily change the aaxx= line to any directory you wish.

# Warnings: This is specifically made to leave your originals alone.
# It is always good idea to backup your originals, though.
# You can never be too safe!
#
# Slavvo67
############################################################################

cd /
aaxx="/root/new1"
mkdir $aaxx
echo "Choose directory of source file"
echo "[Hit Enter] and a graphic box will come up for directory choice"
read aba555
source555=`zenity --file-selection --directory="Select a File"`

case $? in
0)
echo "\"$FILE\" selected.";;
1)
echo "No file selected.";;
-1)
echo "An unexpected error has occurred.";;
esac
cd /
cd $source555
echo "Enter Variable to Change (i.e. FIRM) Do Not Use: &"
read var222
echo "Enter Replacement Words (i.e. Company Name, Inc.) Do Not Use: &"
read repl111
for i in *.rtf; do
awk -v var="$var222" -v var2="$repl111" '{gsub ( var, var2 ) }; {print}' $i >$aaxx/"$i" # NAME in QUOTES
done

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#3 Post by MochiMoppel »

What problems did you have with sed? Seems to be the perfect tool for the job.

Code: Select all

#!/bin/sh
SOURCE_DIR="/mnt/home/rtffiles"
TARGET_DIR="/archive" 
cd "$SOURCE_DIR"
FINDTXT="Jimmy & Hoffa" 
REPLTXT="Bonnie & Clyde" 
REPLTXT=$(sed 's/&/\\&/g' <<< "$REPLTXT") 
for i in *.rtf; do 
sed "s/$FINDTXT/$REPLTXT/g" "$i" > "$TARGET_DIR/$i"
done

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#4 Post by slavvo67 »

Well, your example works fine.

I believe that I was having issues with passing the variables correctly. I'll need to check. I was using a hard code with M4 to properly handle the spaces and the character case. I don't recall with sed if it was the case sensitivity or the spaces that created an issue. I guess it was a syntax issue with me.

What are you doing with this line?

REPLTXT=$(sed 's/&/\\&/g' <<< "$REPLTXT")

Is this specifically to handle the &?

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#5 Post by MochiMoppel »

Yes. The & has a special meaning in sed, so you have to change every & into a \&.
If you would escape the character already in your replace string ("Bonnie \& Clyde") you wouldn't need this line.

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#6 Post by slavvo67 »

Thanks for your script. I didn't ask anyone in the forum because I wanted to figure out a way on my own. As I stated above, I believe my issue with SED was the way I was using the quotes.

Thanks again,

Slavvo67

Post Reply