Linux provides a lot of tools to ease system administrators work. One of them is to replace command which simple search text files to replace string and create a new text file. Replace command comes with MySQL Server packages. IF MySQL is not installed we can not use replace command.

Replace Command

We can use replace command to change a string in text files.

$ replace BEFORE AFTER < YOURCOMMAND > NEWTEXTFILE

BEFORE is the string we search for

is the string we search for AFTER is the new string

is the new string < YOURCOMMAND is our original text file

is our original text file > NEWTEXTFILEis newly created text file with replace string

Beginning and End Of File

We can specify the beginning and end of the file with ^ $

$ replace ^Before After < yourcommand > newtextfile

Replace string Before which is at the beginning of the line

Sed Command

An alternative to replace command is sed. Sed is generally installed by default most of the distributions. So we can replace sed with replace.

$ sed 's/Before/After/g' < yourcommand.txt > newtextfile

OR

$ sed 's/Before//After/g' yourcommand.txt

In this example replace done in place so the new file wouldn’t be created.

Perl Old Friend

We can accomplish replacement with Perl just like below. In the following example, we will change the text Before with the After text.

perl -p -i -e 's/Before/After/g' yourcommand.txt