문제

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?

도움이 되었습니까?

해결책

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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top