1 min read

How to search and replace a string in a file under Linux cli

How to search and replace a string in a file under Linux cli

Another one that is short but very useful. I use this on a daily basis.

Have a look at this line of code:

sed -i 's/what_you_are_looking_for/with_what_you_want_to_replace_it_with/g' File_name

one by one:

-i - sed writes it's output into the same file
s - the substitute command
/// - delimiter
what_you_are_looking_for - well, the work that you are searching for
with_what_you_want_to_replace_it_with - the word that you want to put in place of the old one
g - global replacement flag
File_name - the name of the you will preform the search and replace

So, now you know how to easy replace strings in a file without even opening it :)

For more information about sed:

man sed

Enjoy!