Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use sed replace string pattern with backslash

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!

like image 788
gamebm Avatar asked Jan 20 '13 15:01

gamebm


People also ask

How do you do a backslash in sed?

When in doubt, echo the command: echo sed "s/\//\\\//g" -> sed s/\//\\//g . Btw you can use something else for sed, like 's@/@\\/@g' .

How do you use sed command to replace a string in a file?

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.

How do you replace some text pattern with another text pattern in a file?

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

How do you escape a forward slash in sed?

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.


1 Answers

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.

like image 168
Jonathan Leffler Avatar answered Oct 20 '22 17:10

Jonathan Leffler