MacBook Pro affichant un éditeur de code

Search and replace in the terminal

On Ubuntu it is possible to search and replace a word or phrase with a simple command line

sed -i's/original/new/g' file.txt

Explanation:

sed = Stream EDitor
-i = in-place (save in the original file)

the strings in the command:

s = the replacement command
original = regular expression for the word to be replaced.
new = the text to be placed
g = global (replace all occurrences and not just the first one found)
file.txt = the file name

Leave a comment