I'm sure this has been answered before, but I have a long file with a bunch of expressions of the form \flagThis{Flag this sentence} and I want to replace each of them with Flag this sentence. What's the simplest way to do this? Thanks!

  • 1
    Is it in a TeX or LaTeX document? Would something like detex help?
    – Kusalananda
    Nov 3, 2019 at 7:02
  • It is indeed a latex document. It seems that detex strips everything. If there were a way to get detex to operate on a single specified tex command it would be great, but I couldn't see how to do this.
    – Leo Simon
    Nov 6, 2019 at 3:23

2 Answers 2


Using sed

sed -En 's/\\flagThis\{(.+)\}/\1/gp' file_name


echo \flagThis{Flag this sentence} | sed -En 's/\\flagThis\{(.+)\}/\1/gp'

will output

Flag this sentence

sed -i.backup -r 's/\\[a-zA-Z]+\{([^}]+)\}/\1/g' yourFile

Edits the file in place, creating a backup. Remove -i.backup to test it does what you need first.

You might find this rolls multiple matches on a single line together, so it may be that you want to append \1 with a space or \n (new line).

  • This looks potentially as if it will work well. Unfortunately, my example was too simple, because my sentences enclosed by curly brackets span many lines. Is there any way to extend your example so that it looks for the closing delimiter several lines down?
    – Leo Simon
    Nov 6, 2019 at 3:38
  • Multi-line sed is non-trivial... This answer to a question about exactly that is probably a good bet unix.stackexchange.com/a/152389/40482 -- assuming your file isn't littered with control characters, it should be fairly easy to pick one to use
    – bxm
    Nov 6, 2019 at 10:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .