Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a LaTeX document in which the modifications in the text and equations are done in red color in order for them to be distinguished from unmodified text:


This modification can be a single word, a phrase, a sentence, a paragraph, or an equation. For example:

 \pi \rho \hat{\bf x} \int\omega_{\color{red} \theta}


{\color{red} This work presents the ``numerical study'' of convection in vertical annuli;}

How can I write a perl one-liner which can replace {\color{red} MODIFIED TEXT OR EQUATION GOES HERE.} to MODIFIED TEXT OR EQUATION GOES HERE.? I mean to remove {\color{red} and the right curly brace } throughout the entire LaTeX document.

share|improve this question
A more sensible approach to this is to replace \color{red} with a macro. Then you can just redefine this macro as your see fit. In your latter case, with a blank. The braces most of the time are not functional, so I would not worry about them. – Faheem Mitha Jun 15 '14 at 21:50
Do you mean: \newcommand{\modifcmd}[1]{\color{red} #1}? – Ahmad Jun 15 '14 at 22:09
Something along those lines, yes. Generally strategies that take advantage of the fact that TeX/LaTex are programming languages, albeit unconventional ones, are more robust and effective than alternatives that rely for example on text manipulation. – Faheem Mitha Jun 15 '14 at 22:21
up vote 2 down vote accepted
perl -p -i -e 's/{\\color{red}(.*)}/\1/g' filename

explanation of the flags(from http://stackoverflow.com/a/6302045/330830):

-p: Places a printing loop around your command so that it acts on each line of standard input. Used mostly so Perl can beat the pants off awk in terms of power AND simplicity :-)

-e: Allows you to provide the program as an argument rather than in a file. You don't want to have to create a script file for every little Perl one-liner.

-i: Modifies your input file in-place (making a backup of the original). Handy to modify files without the {copy, delete-original, rename} process.

share|improve this answer

Try this:

$ echo "{\color{red} MODIFIED TEXT OR EQUATION GOES HERE.}" | \
perl -pe 's/\{\\color\{red\} (.*?)\}/$1/g'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.