1

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!

Leo Simon
  • 453
  • 1
    Is it in a TeX or LaTeX document? Would something like detex help? – Kusalananda Nov 03 '19 at 07: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 06 '19 at 03:23

2 Answers2

0

Using sed

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

e.g.

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

will output

Flag this sentence
Prvt_Yadav
  • 5,882
0

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).

bxm
  • 4,855
  • 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 06 '19 at 03:38
  • Multi-line sed is non-trivial... This answer to a question about exactly that is probably a good bet https://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 06 '19 at 10:14