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!
When in doubt, echo the command: echo sed "s/\//\\\//g" -> sed s/\//\\//g . Btw you can use something else for sed, like 's@/@\\/@g' .
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input. txt. The s is the substitute command of sed for find and replace.
`sed` command is one of the ways to do replacement task. This command can be used to replace text in a string or a file by using a different pattern.
if your sed supports it. Then it is no longer necessary to escape the slashes. The character directly after the s determines which character is the separator, which must appear three times in the s command.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With