[Tex/LaTex] search and replace string in all files

editors

I realize that this might not strictly be a LaTeX question but I have still decided to post the question here as other's might have the same problem on occasion.

I am working on a rather large document at the moment that contains a bunch of equations. Instead of writing them as:

    Q^{h}_{Con} = 0.15 \cdot Q^{h}_{Transm} + ...

I would rather do this:

\newcommand{\qCon}{Q^{h}_{Con}}
\newcommand{\qTransm}{Q^{h}_{Transm}}
...
\qCon = 0.15 \cdot \qTransm + ...

Now it is not hard to do this if you have that idea when you start writing on a document. In my case however I already have many pages written (including a nomenclature). Now I would like to replace all mentions of e.g. $Q^{h}_{Con}$ with $\qCon$. Doing this I could make sure that I don't have a typo somewhere and up with a $Q^{h}_{Com}$.

Going through all files on their own is way too complicated and the tex editor I am using only offers search and replace in all opened files. So I have tried to use the Terminal (on Mac) to do so. I have been able to use 'grep' to find all the instances of e.g. $Q^{h}_{Con}$ but could not figure out how to replace it with $\qCon$.

Anyone has an idea?

Best Answer

something like

for i in *.tex
do
sed -i -e 's/Q^{h}_{\([a-zA-Z]*\)}/\\q\1 /g' $i
done

Probably does something. (Save a copy of your files first:-)