Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 20 Sep 2019, 16:41
All times are UTC - 4
 Forum index » House Training » HOWTO ( Solutions )
Batch Find and Replace in RTF _Case Sensitive
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [6 Posts]  
Author Message
slavvo67

Joined: 12 Oct 2012
Posts: 1601
Location: The other Mr. 305

PostPosted: Sat 06 Dec 2014, 21:49    Post subject:  Batch Find and Replace in RTF _Case Sensitive  

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:
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, 13:42; edited 1 time in total
Back to top
View user's profile Send private message 
slavvo67

Joined: 12 Oct 2012
Posts: 1601
Location: The other Mr. 305

PostPosted: Sun 07 Dec 2014, 12:56    Post subject: The Script  

#!/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
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1898
Location: Japan

PostPosted: Sun 07 Dec 2014, 22:29    Post subject:  

What problems did you have with sed? Seems to be the perfect tool for the job.
Code:
#!/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
Back to top
View user's profile Send private message 
slavvo67

Joined: 12 Oct 2012
Posts: 1601
Location: The other Mr. 305

PostPosted: Mon 08 Dec 2014, 00:05    Post subject:  

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 &?
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1898
Location: Japan

PostPosted: Mon 08 Dec 2014, 01:35    Post subject:  

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.
Back to top
View user's profile Send private message 
slavvo67

Joined: 12 Oct 2012
Posts: 1601
Location: The other Mr. 305

PostPosted: Mon 08 Dec 2014, 18:30    Post subject:  

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
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [6 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » House Training » HOWTO ( Solutions )
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1830s ][ Queries: 11 (0.0451s) ][ GZIP on ]