如何使用sed替换字符串模式与反斜杠

时间:2021-04-23 16:51:00

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

换句话说,如果有任何括号括号或括号,我想要\ right,如果有的话,它应该被\ rightarrow替换。简而言之,我正在尝试将大量短缺的google doc方程转换为适当的LaTeX格式。我想出的是以下内容

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

它工作正常,但它不会按预期更改\ right \ into \ rightarrow \。我的测试输入tempfile1如下

\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...

注意到\ right \ part没有正确完成。然后我添加了以下一行,认为它会明确地拿起剩下的东西,然而,它没有按预期工作,现在我真的很困惑......

sed -i 's/\\right\\/\\rightarrow\\/g' $tempfile1 #why this does not work

Thanks a lot in advance!

非常感谢提前!

1 个解决方案

#1


3  

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).

第一个匹配读取\ right \并用\ rightarrow \替换它;扫描恢复时出现问题,它从第二个右边的r开始,而不是反斜杠(这是前一个匹配的一部分)。

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.

重新扫描通过再次启动ab initio来处理第二个\ right \序列。

#1


3  

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).

第一个匹配读取\ right \并用\ rightarrow \替换它;扫描恢复时出现问题,它从第二个右边的r开始,而不是反斜杠(这是前一个匹配的一部分)。

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.

重新扫描通过再次启动ab initio来处理第二个\ right \序列。