0

I've got a LaTeX file containing

\newcommand{\revision}{value}

And during CI I want to do the following:

REV=${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}
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
\newcommandvaluetest

Why does this happen?

4
  • can u echo $REV and share the result
    – Siva
    Commented Mar 13, 2020 at 9:18
  • That part works $ echo $REV 758d59ea Commented Mar 13, 2020 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
    Commented Mar 13, 2020 at 9:53
  • Can you provide an example of the expected output? Commented Mar 13, 2020 at 9:59

1 Answer 1

3

Try this,

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

\newcommand{\revision}{577f813d}
  • 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
1
  • 2
    This is it, I just placed the capture group in the wrong place. Without extended regex: sed 's/\({\\revision}{\)[^}]*/\1'"$REV"'/' Commented Mar 13, 2020 at 10:53

You must log in to answer this question.

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