here is a practical situation.I facing some of my files where there is a common string in these files.I usually will open vi and do some thing like
:%s/text/replacement/g
and I have to do it in many files many many times. Is there some easy way to do it or some sort of alias that I can create for the same?
You can use find for finding the target files and then pipe it to sed:
Only replace in .txt files:
(from this blog post)
Ah yes, time for a perl oneliner. How about this?
or maybe you want to change all the *.txt files in the current directory? In that case, how about combining it with GNU parallel:
With parallel, the
:::
means use eveything after this as input files.or, to change all the txt files in the current directory and below:
Note the
-i.bak
saves a backup copy of every file that you change, in case you make a mistake. You can leave that out if you are feeling dangerous.Anyway obviously the
sed
answer is the standard one, but there are some fun alternative ways to do it too. Also if you have a lot of files gnu parallel will be significantly faster as it runs multiple copies in parallel (as one would hope, given the name).Check out sed, something like this: