Find and replace text in a file

find + replace = sed

In order to replace text in a file from the command line on a linux / ubuntu computer , you need to use the sed command. This command is definitively already install on your linux system as it is part of default command installed in linux distributions. sed stands for Stream EDitor.

The general command line will be:

sed -i ‘s/TextToFind/TextToReplaceBy/g´ FileWhereItHappend.txt

  • -i : to update the file you are working on (FileWhereItHappend.txt). If you do not put it, the command will spit out the result on the terminal window.
  • ‘ … ‘ : inside parameters for the find and replace job
  • s : The substitute command of sed for find and replace
  • / : is used as the separator of inside parameters. You can also the + (plus sign), this is useful when the / is part of the text that you need to find and/or replace.
  • g : Define the find and replace as a global job in the file. Without it, it will only find and replace the first occurrence of the text.

Leave a Reply

Your email address will not be published. Required fields are marked *

20 − 19 =