Making simple edits, such as replacing text, to multiple files is a common chore. Thankfully it is something that can easily be automated. There are several different approaches, depending upon the tools you prefer. Here we’ll look at two. The way that I generally perform simple search and replace operations on multiple files is to use Perl, from the command line.

The reason I do that is because the common way of using Perl to script replacements allows a simple backup to be made of the original file, so if I make a mistake I know I can revert it easily.

In general the way to make changes is to run:

perl -pi.bak -e “s/Old text/New text/g” file1 file2 file3

So, for example, to change “Bob” to “Steve” on all HTML files in the current directory I would execute:

perl -pi.bak -e “s/Bob/Steve/g” *.html

Explaining this command is pretty simple, we’re running the perl interpreter with several flags:

* -p tells perl to assume a simple loop around the code we give it to run.
* -i.bak tells perl that we wish to create backups of the files we modify with the suffix .bak.
* -e "..." tells perl that we wish to execute the code inside the quotes.
* And finally the file list is the list of files that we wish to operate upon.

Because there is the full Perl environment available we can modify the code we’re executing (the “s/Bob/Steve/g” section) to cause it to behave differently, for example we can do the replacements in a case-insensitive manner by adding “i” thus:

perl -pi.bak -e “s/Bob/Steve/gi” *.html

(One thing to be aware of, because this is perl code you will need to escape special characters such as “.”, “?”, “*”, or “/”. This can be achieved by preceding them with the “" character. eg “Steve?” becomes “Steve\?” - this is because these characters are used in Perl’s regular expressions - a very powerful facility).

Once this has finished we’ll be left with our modified .html files along with the backups of the original files with the suffix .html.bak. If you’re happy with the edits you can simply remove the backups:

rm *.html.bak

If something went wrong you can easily rename multiple files to fix the problem. For example to revert the edit and replace the modified versions of the files with the original backups run:

for i in *.html.bak ; do mv $i ${i/.bak/} ; done

Another approach to replacing multiple strings across files, with built in support for recursive operation is the rpl package.

This is a simple tool which allows you to perform similar replacements, and limit them to particular file suffixes.

Once it’s installed (via “apt-get install rpl”) you invoke it like this:

rpl “Old String” “New String” file1 file2 … fileN

You can run it recursively with the “-R” option, as well as limiting it to working recursively upon only files of a given suffix. For example the following command will recursively make changes only to files which are named *.html:

rpl -R -x .html “Steve” “Steve Kemp” *

There are other options you can use such as “-i” to specify that the searching is case-insensitive, and “-s” to simulate the search - only showing you what would be changed without actually making any replacements.

There are many other ways of editing multiple files available to Debian users, from Emacs, Vi-clones, and other scripting solutions. But the two solutions described above work well for me.

With a little bit of thought a lot of common operations can be automated - for example I recently wished to add a header file, and footer file, to each file in the current directory.

Whilst no existing tool seemed likely to help I realised I could do the job with the standard cat command and a simple loop. Writing the simple loop in the bash shell was only a minutes work:

for i in *.tmpl; do cat ../header.txt $i ../footer.txt > $ mv $ $i; done

Here we use the cat tool to add the header, the original file, and the footer and save it to a temporary file. Then we rename the temporary file - and overwrite the original input. Job done.

With a little thinking many common text editing jobs can be accomplished in a variety of ways.

Source: Autor’s Home Page: