rpl is a very useful command used for bulk string replacement in files. Basic usage is to specify two strings and one or more filenames or directories on the command line. The first string is the string to replace, and the second string is the replacement string. Another Linux command used to replace string is sed.
To instal rpl on Ubuntu or Debian, use
apt install rpl
To install from source
cd /usr/local/src wget http://downloads.laffeycomputer.com/current_builds/rpl-1.4.1.tar.gz tar -zxvf rpl-1.4.1.tar.gz cd rpl-1.4.1 ./configure make make install
Here are some of the command-line options for rpl command.
-i, --ignore-case = Ignore the case of old_string. -w, --whole-words = Make old_string match only on word boundaries. -b, --backup = Move the original files to filename~ before replacing them. -q, --quiet = Quiet mode. -v, --verbose = Verbose mode. -s, --dry-run = Simulation mode, no files are changed. -R, --recursive = Recurse into subdirectories. -e, --escape = Expand escape sequences in old_string and new_string. Examples of escape sequences are '\n' (new-line), '\t' (tab), '\x42' (hexadecimal number 42), '\033' (octal number 033). -p, --prompt = Prompt for confirmation before replacing each file. -f, --force = Ignore errors when trying to restore permissions and file ownership. -d, --keep-times = Keep modification times when replacing files. -t, --use-tmpdir = Use a temporary directory for storing temporary files, usually the value of the environment variable TMPDIR. The default is to put temporary files in the same directory as the file being modified. -a, --all = Do not ignore files and directories starting with.
Examples
Replace string in one file.
rpl "OLD_STRING" "NEW_STRING" FILE_NAME
Replace in multiple files
rpl "OLD_STRING" "NEW_STRING" FILE_NAME_1 FILE_NAME_2
To replace all occurrences of CAT with RAT run the following command, it will go through all files and do the replacement.
rpl -R -x .php -x .html -x .htm 'CAT' 'RAT' *
-x specify file extensions you need to replace. In above case, it only replace in files with .php, .html and .htm file extensions.
-R option is used to change recursively.
Replace all occurences of ”F” (on word boundaries) with ”A” in all text files under the grades/ directory:
rpl -Rwd -x'.txt' 'F' 'A' grades/
See sed.