Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

How to use rpl command in linux?

4 Answer(s) Available
Answer # 1 #
  • Use Stream EDitor (sed) as follows:
  • sed -i 's/old-text/new-text/g' input.txt.
  • The s is the substitute command of sed for find and replace.
  • It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
[5]
Edit
Query
Report
D.N Howard
LACQUERER
Answer # 2 #

The rpl utility can be installed from the official Ubuntu Repositories using the apt-get command as follows :

apt-get install rpl

The basic way to use rpl is to provide two strings followed by one or several filenames or folders.The first string is the one to replace while the second is the new string, .i.e the replacement string.

Read: How to use the APT command on Ubuntu/Debian Linux systems

One file

The following command will replace a string string_to_replace with a new one new_string in a file named file_name.txt.

rpl string_to_replace new_string file_name.txt

If the string has spaces in it, it should be embedded in quotation marks. To ignore case, you should use the -i option . For whole words, the -w option should be applied.

Multiple files

It is also possible to specify multiple files as shown below :

rpl -i -w “string to replace” “new string” file1.txt file2.txt

To indicate the extensions of the files, you should use the -x option. Recursive search in the directory can be implemented using the -R option as shown below :

rpl -x .txt -x .html -R string_to_replace new_string file*

Interactive mode using the -p can be used to search/replace as well.

For more on rpl , visit this page.

Read: How to exit Vim editor

The sed tool (stream editor), is a powerful text manipulation utility which is used for text substitution. It can also carry out other text operations like deletion, insertion, search etc. The sed tool allows you to edit a whole file without having to open it. Regular expressions can also be used with sed.

The syntax is as follows :

sed ‘s/string1/string2/g’ input_file.  [linux replace string in file]

Simply put, this will replace all occurrences of string1 with string2 in the file input_file.

The ‘/g’ option allows sed to make a global replace operation i.e. to replace all occurrences of string1. If ‘/g’ is not used, then only the first occurrence is changed.

Read: How to use grep command in Linux

The command below will replace the word ‘building’ with the word ‘house’ in the file design.txt

sed ‘s/building/house/g’ design.txt.                [sed Linux examples]

To update the file immediately, use the -i option as follows :

sed -i ‘s/building/house/g’ design.txt

To match all different cases of the word ‘building’, add the I option as follows :

sed -i ‘s/building/house/gI’ design.txt

The following will make the replacement in all files whose name contains text

sed -i — ‘s/building/house/g’ *text*.          [sed replace sting in file]

The following will do the replacement in all files whose name ends with txt

sed -i — ‘s/building/house/g’ *.txt*

The command below will perform a recursive search and carry out the replacement for all files whose name contain ‘text’ in the current and all subdirectories :

find . -type f -name “*text*” -exec sed -i ‘s/building/house/g’ {} +

In case you want to make a replacement of a word with a new one within a line that contains another word. For example, the command below will find the word ‘building’ and will replace it with ‘house’ if the corresponding line contains the word ’construction’:

sed -i -e ‘/construction/s/building/house/’ design.txt

To replace any of word1, word2 or word3 with newword, proceed as follows :

sed -Ei ‘s/word1|word2|word3/newword/g’ file

You can also combine sed commands as follows:

sed -i ‘s/word1/newword1/g; s/word2/newword2/g; s/pete/tom/g’ file

Be aware that the replacement order matters , for instance

sed ‘s/word1/newword1/g; s/newword1/newword3/g’

[4]
Edit
Query
Report
Mrunal Irshad
Network Administrator
Answer # 3 #

Procedure for searching and replacing strings in multiple files across multiple directories

Version: 2; August 14, 2009;

This note is intended to help the X-Ray Facility (XRF) users search and replace string1 by string2 that occurs in many places in multiple files across multiple directories using the Linux/UNIX commands find, exec and sed.  Copy of this Note will be posted in XRF Resources page shortly after receiving suggestions and corrections from the users.  This note updated on August 14, 2009 to include rpl and was first written in November 29, 2007.

Find, exec, & sed are UNIX/Linux commands that can be utilized to do routine work with ease.  The problem I needed to solve was to replace string1 by string2 across multiple files across multiple directories quickly.  A quick search of the Internet yielded some clues about how to proceed.

TSomasundaram | November 06, 2007 | Search and replace several lines in several files in several directories. Google search term: linux replace recursive

Link1: tips.webdesign10.com/recursively-find-and-replace-linux

Recursively Find and Replace in GNU/Linux | 2007, February 6 - 11:42pm — WebDesign10

Web designers often link to index.html in directories throughout a Web site — or even worse, only partially throughout a Web site. If you are dealing with a static HTML site, it should be fairly easy to fix with this recipe.

The following line in the GNU/Linux terminal will find and replace (delete) the text index.html recursively in all files, starting in the current directory:

find ./* -type f -exec sed -i 's/index.html//g' {} \;

Link2: www.jonasblog.com/2006/05/search-and-replace-in-all-files-within-a-directory-recursively.html

So, to search recursively through directories, looking in all the files for a particular string, and to replace that string with something else (on Linux) the following command should work:

find ./ -type f -exec sed -i ’s/string1/string2/’ {} \;

Where string1 is the search and string2 is the replacement.

Then I started my own trials and took some help from Michael Zawrotny, IMB System Manager.  What I wanted to do was to update an old URL that was part of the template to a new location.  Since this URL was found in almost all .html files for www.sb.fsu.edu/~soma and www.sb.fsu.edu/~xray, I needed first to back-up the old html files so that I will not lose my webpages.  Then I tested the code with grep rather than sed.  This way I will see whether code was working before implementing it.  Then I tried in on a sub-directory before doing it in the whole site.

1) find ./ -type f -exec grep -i '~webguide' {} \;

Explanation: Here we are using the find command with –type f option to get only files and NOT directories. Then we are using –exec option of find with grep as an operator.  The grep command has option –i and is looking for a pattern ‘~webguide’.  Then we have couple of symbols that are part of exec command {} \;.  Note the combination of curly braces, an empty space, forward slash, and a semi-colon.  This combination has to be written exactly as shown (red-arrows indicate empty space).

2) find ./* -exec grep -i -H '\-2005’ {} \; | more

Explanation: Here we are using the find command with wild card.  Then we are using –exec option of find with grep as an operator.  The grep command has option –i and –H options looking for a pattern ‘\-2005’.  What I am looking for is actually ‘-2005’, but since ‘-’ is a special character,  I have to escape it with ‘\’, the escape character back-ward slash.  I am also using –H option to get the filenames under grep.  Once again, note the combination of curly braces, an empty space, forward slash, and a semi-colon.  This combination has to be written exactly as shown (red-arrows indicate empty space).

3) find ./ -iname \*.htm\* -exec grep -i “\-2005” {} \;

Explanation: Here we are using the find command with -iname \*.htm\* option. Then ‘\*’ is escape character and the wildcard combination. ‘.htm\*’ is to capture all .htm, and .html files and escaping the special character ‘*’.  Rest of the command is same as before.

4) find ./ -iname \*.htm\* -exec sed -i 's/\-2005/\-2007/g' {} \;

Explanation: Here we are using the find command with -iname \*.htm\* option. Then ‘\*’ is escape character and the wildcard combination both in front and back of‘.htm’ is to capture all .htm and .html files.  Then using sed to replace all ‘-2005’ by ‘-2007’.  Note the special format for sed and used with –i option where s stands for substitute and g stands for global (all occurrences), and /string1/ /string2/ delimit string2 is substituted for string2. Once again we have to escape ‘the dash’ in front of 2005 with escape character (‘\-’)

5) find ./ -iname \*.htm\*  -exec grep -i '~webguide' {} \;

Explanation: Here we are simply checking to make sure all the replacements have been done.  The above command should give no output if everything has worked as planned.

UNIX/Linux command find can be utilized to do routine work with ease when the strings to be replaced string1 by string2 are small.  But when the strings are long I found rpl to be more useful.  Rpl can be used to replace strings across multiple files across multiple directories as well.

Rpl utility may not be part of the original distribution and so one may not find it in all systems.  So one has to get it and install it.  For RedHat system I found the rpm in the following location: http://www.laffeycomputer.com/rpl.html

If one has Debian or Ubuntu system then you get the rpl utility using apt-get and install like the following:

soma@xyz:~$ which rpl

bash: type: rpl: not found

soma@xyz:~$ sudo apt-get install rpl

…  … …

Unpacking rpl (from .../archives/rpl_1.5.5-1_all.deb) ...

Setting up rpl (1.5.5-1) …

soma@xyz:~$ which rpl

rpl is /usr/bin/rpl

Once rpl is installed one can replace long strings on multiple locations, on multiple files, on multiple directories.  I needed to replace a location for all my .html files with new location on all those files (since the old location was no longer valid).  I went about first running rpl in verbose and simulation mode.  After making sure everything is OK, I went ahead and replaced the locations.  Things went well quickly.

1) rpl –vsRd –x ‘.html’ ‘www.site.com/loc/file’ ‘www.site.us/~ab/loc2’ *

Explanation: Here we are using the rpl command with –vsRd and –x ‘.html’ options to get only files that have extension ‘*.html’. The option –v means verbose mode, –s means we running the simulation (and not actually doing the replacing yet), –R means we are using the recursive mode (looking for files in ./ and its child directories, and –d means we are keeping the original time of creation (maintaining old time-stamp). Here ‘www.site.com/loc/file’ is the original string that needs to be replace by ‘www.site.us/~ab/loc2’, and finally ‘*’ means we are doing on all files (redundant) since we are specifying ‘-x .html’.

After making sure everything is OK, I went ahead and replaced the locations.  Things went well quickly.

2) rpl –vRd –x ‘.html’ ‘www.site.com/loc/file’ ‘www.site.us/~ab/loc2’ *

Explanation: Here we are using the rpl command with –vRd and –x ‘.html’ option meaning we are no longer using simulation mode but actually doing the replacement.

[1]
Edit
Query
Report
Suparn Bhalchandra
ACID SUPERVISOR
Answer # 4 #

To instal rpl on Ubuntu or Debian, use

To install from source

Here are some of the command-line options for rpl command.

Replace string in one file.

To replace all occurrences of CAT with RAT run the following command, it will go through all files and do the replacement.

-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:

[0]
Edit
Query
Report
Sathish Malone
CORE EXTRUDER