Sometimes we want to replace text within a file, for example, changing everything that says "my family" to "us" in Document.txt . To do this, we can use our preferred text editor (Kate, Gedit, Sublime Text, etc.), but it's always good to know alternatives, other ways to achieve the same result.
It turns out that with regular expressions this can be achieved; the `sed` command is simply magnificent, it has endless variations and options, but… for those who only want to replace text in the least complex way possible , I bring you this much simpler variant:
Following the example, we have the file Document.txt located in $HOME/Document.txt with the following text:
My family is like everyone else's, like yours or your neighbor's. In fact, my family tries to be responsible people in the eyes of society, politically and socially correct. However, there's a black sheep in my house, and that's my role, LOL! I've always been the one who isn't afraid to say what I think (even if it's not socially acceptable), although in reality, my family is fed up with the
messed-upsociety of today.
The following command will change " my family " to " us ":
perl -pi -e "s[mi familia][nosotros]g" $HOME/Documento.txt
It's that simple 
So the text would be:
(…) we 're like everyone else's, like yours or your neighbor's. In fact, we try to be responsible people in the eyes of society, politically and socially correct. However, there's a black sheep in my house, and that's my role, LOL! I've always been the one who isn't afraid to say what I think (even if it's not seemingly correct in the eyes of society), although in reality my family is fed up with the
filthof today's society.
The syntax is:
perl -pi -e "s[lo-que-quiero-cambiar][lo-nuevo-a-poner]g" archivo-en-el-cual-reemplazar
The command may seem a bit complex, let's break it down a bit and explain it in a slightly simpler way 
- perl : What we will use, perl
- -pi : PÃ, how Ï€ (the approximate value of 3.14)
- -e : The E imagine it for «e to execute» (I.e.
- Then in quotes » we open with a s and we close with a g: "sg" …imagine the S for Start and the G for Game Over 
- Turn between the sg we must put two sets of brackets: [] y [], being that way: "S [] [] g"
- Inside the first brackets as I said above, the text that we want to remove and replace with a new one will go, while in the second set of brackets the new text to put will go, following the example: "S [my family] [we] g"
Simple isn't it?
The explanation is actually much more extensive than what is needed for the command, it is quite simple to understand 
Perl is much, much more than a way to replace text, it is an entire language 
If you want to learn more about it, install the perl-doc package and take a look at everything it allows; it's simply a different universe.
By the way and to close, assuming you want to find all the .txt files you have in $ HOME / Documents / and in each one replace "my family" with "us", you can join find + perl to achieve this:
find $HOME/Documentos/ -name *.txt -exec perl -p -i -e "s[mi familia][nosotros]g" {} \;
Or using a wildcard:
perl -p -i -e "s[mi familia][nosotros]g" $HOME/Documentos/*.txt
The problem with this second variant is that it will not replace files that are located in subfolders 
Anyway, I hope you found it interesting. For me it was really useful, I can now replace text directly from the terminal… great! 