Global search and replace in multiple files

Searching and replacing a bit of text in a lot of files is a very common problem. Lots of tools exist to get it done. On unix systems, awk is one favorite, and I found this webpage with a simple search and replace example.
Unfortunately, I wanted to search for \prob and replace it with %\prob. The difficulty is that the backslash character is “special”, so you have to escape it with other backslashes.

Here is my updated example command to run in each directory:

awk ‘{gsub(/\\prob/,”%\\prob”, $0); print > FILENAME}’ *.tex

It does a global substitution for the regex \\prob with string %\prob in the
whole input file ($0) for every file that ends in .tex

Leave a Reply

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