The sed
command (short for "stream editor") is a utility in Linux and Unix-like operating systems that is used to perform text transformations on an input stream (such as a file). It can be used to find and replace text in one or more files.
Here is the basic syntax for using sed
to find and replace text in a file:
sed 's/old-text/new-text/g' input-file > output-file
This command will search the input-file
for the string old-text
and replace it with new-text
, writing the result to the output-file
. The g
at the end of the search and replace pattern specifies that all occurrences of old-text
should be replaced, not just the first one.
You can also use the -i
option to edit the file in place, without creating a separate output file:
sed -i 's/old-text/new-text/g' input-file
If you want to search and replace text in multiple files, you can use the find
command to search for the files, and xargs
to apply the sed
command to each file:
find /path/to/files -type f -print0 | xargs -0 sed -i 's/old-text/new-text/g'
This command will search for all regular files in the /path/to/files
directory, and apply the sed
command to each file, replacing old-text
with new-text
.
It's important to be careful when using sed
, as it can overwrite files if used incorrectly. It's a good idea to make a backup of the files you are going to modify before using sed
.