Linux replace command

Updated: 05/04/2019 by Computer Hope
replace command

On Unix-like operating systems, the replace command makes modifications to strings of text in files or the standard input.

Description

replace looks for all occurrences of string from and replaces it with string to. You can specify one or more pairs of strings to search/replace in a single replace command.

Use the -- option to indicate where the string-replacement list ends and the file names begin. In this case, any file named on the command line is modified in place, so you may want to make a copy of the original before converting it. replace prints a message indicating which of the input files actually got modifies.

If the -- option is not given, replace reads the standard input and writes to the standard output.

Syntax

replace from to [from to] ... -- file_name [file_name] ...
replace from to [from to] ... < file_name

Options

-?, -I Display a help message and exit.
-#debug_options Enable debugging.
-s Silent mode. Print less information what the program does.
-v Verbose mode. Print more information about what the program does.
-V Display version information and exit.

Examples

Tip

The replace command is primarily used with msql2mysql although can still be used for a way to replace text within a file or input. If you are looking for a tool in Linux to replace text in one or more files, try the sed ("stream editor") command.

replace "computer" "hope" -- example.txt

Replace the word computer with the word hope in example.txt, overwriting the original file with the changes.

sed — A utility for filtering and transforming text.