Shell Scripting on Amazon




































































Find & Replace with RPL and DIALOG

( BACK to article ) Here is the script:-

#!/bin/bash
while [ 0 ]; do
dialog --title "Find And Replace" \
  --radiolist "Choose one of the following or press <Cancel> to exit" 18 45 8 \
   "1" "Open Help File" off\
   "2" "Replace Text In File" off\
   "3" "Replace Text (All Files In Folder)"  off\
   "4" "Replace Text (Recursive)"  ON 2>/tmp/fr
   if [ $? = 1 ]; then
   rm -f /tmp/fr
   clear
   exit 0
   fi
R="`cat /tmp/fr`"
if [ $R = "1" ]; then
dialog  --title 'Help' --msgbox 'OPTION 1. prints this help file - OPTION 2. will replace all instances of a text string with a new string in a given file - OPTION 3. will replace all instances of a text string with a new string in all files in a given directory. - OPTION 4. will replace all instances of a text string with a new string in all files in a given directory and all its sub-directories. WORKS WITH TEXT AND HTML FILES ONLY! You will need to enter the full path to all files and folders. This front end script should work equally well for single and multiple word substitutions . RPL is a command line program and it is capable of much more than this. In order to acquaint yourself with the full range of its capabilities consult the manual - man rpl. Enjoy!' 22 75
elif [ $R = "2" ]; then
dialog --inputbox "Enter Old Text:" 8 40 2>/tmp/aa
if [ $? = 1 ]; then
   rm -f /tmp/aa
   clear
   exit 0
fi
AA="`cat /tmp/aa`"
rm -f /tmp/aa
dialog --inputbox "Enter New Text:" 8 40 2>/tmp/bb
if [ $? = 1 ]; then
   rm -f /tmp/bb
   clear
   exit 0
fi
BB="`cat /tmp/bb`"
rm -f /tmp/bb
dialog --inputbox "Enter The File Name - Full Path:" 8 40 2>/tmp/cc
if [ $? = 1 ]; then
   rm -f /tmp/cc
   clear
   exit 0
fi
CC="`cat /tmp/cc`"
rm -f /tmp/cc
touch /home/username/.5r6t7ykl ;
echo \#!/bin/bash > /home/username/.5r6t7ykl ;
echo rpl -q  \'$AA\' \'$BB\' $CC >> /home/username/.5r6t7ykl ;
chmod 755 /home/username/.5r6t7ykl ;
/home/username/.5r6t7ykl ;
rm -f /home/username/.5r6t7ykl
dialog  --title 'Success' --msgbox  'DONE - Click OK to continue.' 8 40 ;
elif [ $R = "3" ]; then
dialog --inputbox "Enter Old Text:" 8 40 2>/tmp/ee
if [ $? = 1 ]; then
   rm -f /tmp/ee
   clear
   exit 0
fi
EE="`cat /tmp/ee`"
rm -f /tmp/ee
dialog --inputbox "Enter New Text:" 8 40 2>/tmp/ff
if [ $? = 1 ]; then
   rm -f /tmp/ff
   clear
   exit 0
fi
FF="`cat /tmp/ff`"
rm -f /tmp/ff
dialog --inputbox "Enter The Folder Name:" 8 40 2>/tmp/hh
if [ $? = 1 ]; then
   rm -f /tmp/hh
   clear
   exit 0
fi
HH="`cat /tmp/hh`"
rm -f /tmp/hh
touch /home/username/.8n9d2dfg ;
echo \#!/bin/bash > /home/username/.8n9d2dfg ;
echo rpl -q  \'$EE\' \'$FF\' $HH/* >> /home/username/.8n9d2dfg ;
chmod 755 /home/username/.8n9d2dfg ;
/home/username/.8n9d2dfg ;
rm -f /home/username/.8n9d2dfg ;
dialog  --title 'Success' --msgbox  'DONE - Click OK to continue.' 8 40 ;
elif [ $R = "4" ]; then
dialog --inputbox "Enter Old Text:" 8 40 2>/tmp/ii
if [ $? = 1 ]; then
   rm -f /tmp/ii
   clear
   exit 0
fi
II="`cat /tmp/ii`"
rm -f /tmp/ii
dialog --inputbox "Enter New Text:" 8 40 2>/tmp/jj
if [ $? = 1 ]; then
   rm -f /tmp/jj
   clear
   exit 0
fi
JJ="`cat /tmp/jj`"
rm -f /tmp/jj
dialog --inputbox "Enter The Folder Name - Recursive" 8 40 2>/tmp/kk
if [ $? = 1 ]; then
   rm -f /tmp/kk
   clear
   exit 0
fi
KK="`cat /tmp/kk`"
rm -f /tmp/kk
touch /home/username/.4s3q1bnm ;
echo \#!/bin/bash > /home/username/.4s3q1bnm ;
echo rpl -qR  \'$II\' \'$JJ\' \'$KK\' >> /home/username/.4s3q1bnm ;
chmod 755 /home/username/.4s3q1bnm ;
/home/username/.4s3q1bnm ;
rm -f /home/username/.4s3q1bnm ;
dialog  --title 'Success' --msgbox  'DONE - Click OK to continue.' 8 40 ;
fi
done

See this page for generic instructions on making scripts run. You will need to install 'rpl' and 'dialog'. If you are running Ubuntu/Debian both can be found in the repositories.  You will also need to replace 'username' with the name of your home directory.