Question

I am trying to use a script to insert \cbstart{} and \cbend{} tags in a latex file when new text occurs.

end=$(( $start + $diff ))
sed -i "" "${start}s/^/\\\\cbstart{} /" "$OUT"
sed -i "" "${end}s/$/ \\\\cbend{}/" "$OUT"

However, when a \cbend{} tag is placed on a new line that contains a comment at the end:

\cbstart{} Example text. %Example commment \cbend{}

Commenting out the end tag. Is there a way to use sed to place the \cbend{} tag before a % or on a new line?

Was it helpful?

Solution

Perhaps:

sed -i ""  "${end}s/\\( %.*\\|$\\)/ \\\\cbend{}\\1/" "$OUT"

With saner escaping

sed -i ""  "${end}"'s/\( %.*\|$\)/ \\cbend{}\1/' "$OUT"

I did test this before posting:

end=""
{ echo foo; echo "bar % with comment"; } | 
sed "${end}"'s/\( %.*\|$\)/ \\cbend{}\1/'
foo \cbend{}
bar \cbend{} % with comment

Since the OP is on a Mac, the sed incantation required is:

sed -i "" -E "${end}"'s/( %.*|$)/ \\cbend{}\1/' "$OUT"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top