Substitute one word (or a pattern) for another through a whole file from the command line
You can substitute one word for another through a whole file by running:
$ sed s/oldword/newword/g inputfile > outputfile
What’s happening in this command?
First you tell the terminal to run
sed, the name of the programme (more info below).
Next you tell sed that you want to run a replacement of ‘oldword’ by ‘newword’.
We start with
s, which stands for substitute.
/’s are just separators.
g tells sed to apply the substitution on all occurrences of ‘oldword’ throughout the file. If you omit
g, sed will only perform the substitution on the first encounter of ‘oldword’, leaving other ‘oldword’ unchanged.
inputfile is the file which you have with ‘oldword’.
outputfile will be a file created with ‘newword’, once the substitution procedure is completed.
> is what is called a redirection, that says from an input file to an output file.
What is sed?
Sed is a stream editor, that is a programme to perform basic text transformations on an input stream, a file for example. There is a lot more you can do with sed; if you have a repetitive text editing tasks to run, sed might be able to help you. For example, you can delete lines that contains a specific keyword, or add a line to a file (e.g. add an item to a to-do list).
You can run
$ man sed or
$ info sed to learn more or see some examples.
text processing personal computing gnu linux trisquel command-line interface (cli)