Reputation: 410
I want to do replace for the following patterns (the foregoing rule has higher priority)
\right) -> remain unchanged
\right ) -> remain unchanged
\right] -> remain unchanged
\right ] -> remain unchanged
\right} -> remain unchanged
\right } -> remain unchanged
\ri) -> \right)
\ri -> \rightarrow
\right -> \rightarrow
In other words, if there is any parentheses bracket or brace, I want to have \right, if anything else, it should be replaced by \rightarrow. In short, I was trying transform a lot of shorthanded google doc equations with into proper LaTeX format. What I came up with was the following
sed -i 's/\\ri\([^g]\)/\\right\1/g' $tempfile1 #first step substitution
sed -i 's/\\right\([^])}>|a]\)/\\rightarrow\1/g' $tempfile1
sed -i 's/\\right \([^])}>|a]\)/\\rightarrow \1/g' $tempfile1
It works ok except it does not change \right\ into \rightarrow\ as expected. My test input tempfile1 is the following
\ri\right\right \right)\right]\right }\right )\ri \right ]\righta \al \\
It goes into
\rightarrow\right\rightarrow \right)\right]\right }\right )\rightarrow \right ]\rightarrow \alpha \\
Noting that the \right\ part was not done correctly. Then I added the following line, thinking that it will explicit pick up what was left, however, it does not work as expected and now I am really confused...
sed -i 's/\\right\\/\\rightarrow\\/g' $tempfile1 #why this does not work
Thanks a lot in advance!
Upvotes: 7
Views: 2781
Reputation: 753765
The trouble occurs when the expression:
sed -i 's/\\right\([^])}>|a]\)/\\rightarrow\1/g'
is applied to:
\right\right\
The first match reads \right\
and replaces it with \rightarrow\
; the problem occurs when the scan resumes, it starts at the r
of the second right
, not with the backslash (that was part of the previous match).
The simple trick is to repeat the command...
sed -i -e 's/\\right\([^])}>|a]\)/\\rightarrow\1/g' \
-e 's/\\right\([^])}>|a]\)/\\rightarrow\1/g'
The rescan deals with the second \right\
sequence by starting ab initio again.
Upvotes: 3