Replace text in files

3 April 2016

It’s pretty comfortable to search and replace text in files using perl and regular expressions. Using one single command we can search, replace and create backups of all the files at once.

Let’s create a file first:

echo "perl is Kewl" >> regtext.txt;

Now let’s do the trick:

perl -p -i.bak -e 's/k|Kewl/cool/g' regtext.txt

This command will search for Kewl in regtext.txt and will replace it with cool and it will create a backupfile called regtext.txt.bak.

[ Perl  One-Liner  Tricks  Shell  ]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 Unported License.

Copyright 2015-present Hoti