I've got a LaTeX file containing


And during CI I want to do the following:

sed -i 's/{\\revision}{\(\w*\)}/\1'"$REV"'/' variables.tex

expecting something along the lines of \newcommand{\revision}{577f813d}

Unfortunately this happens:

> REV=test sed 's/{\\revision}{\(\w*\)}/\1'"$REV"'/' variables.tex

Why does this happen?

  • can u echo $REV and share the result – Siva Mar 13 '20 at 9:18
  • That part works $ echo $REV 758d59ea – Matthias Brandt Mar 13 '20 at 9:26
  • 3
    Looks like it does what is programmed: eliminate the {s and }s, and the \revision string, then adds the test string. – RudiC Mar 13 '20 at 9:53
  • Can you provide an example of the expected output? – Paulo Tomé Mar 13 '20 at 9:59

Try this,

sed -e "s/\({\\\revision}\){\w*}/\1{$REV}/" variables.tex

  • sed required two black slashes to escape backslash.
  • We should back-reference the {\\\revision} instead of {\w*}
  • finally required a curl brace around REV as per our expected output
  • 2
    This is it, I just placed the capture group in the wrong place. Without extended regex: sed 's/\({\\revision}{\)[^}]*/\1'"$REV"'/' – Matthias Brandt Mar 13 '20 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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