Replace text with sed


Given a file with the content:

$ cat file.txt
Lorem ipsum dolor

Let’s say you want to replace or with OR.

To do that for the first match without modifying the file:

$ sed 's/or/OR/' file.txt
LORem ipsum dolor

To do that globally and without modifying the file:

$ sed 's/or/OR/g' file.txt
LORem ipsum dolOR

To do that globally with the existing file being modified:

$ sed -i '' 's/or/OR/g' file.txt
$ cat file.txt
LORem ipsum dolOR

To do that globally with a new file being created:

$ sed -i .bak 's/or/OR/g' file.txt
$ cat file.txt.bak
LORem ipsum dolOR