7

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!

2
  • Surely you could combine your 3 scripts into one using sed -i -e '...pattern1...' -e '...pattern2...' -e '...pattern3...' .... For kilobyte-size files, three passes probably don't matter much, but for multi-megabyte files and bigger, one pass vs three can make a big difference. Jan 20, 2013 at 15:58
  • I see, thx. The typical file size for me is less than 50K (normal LaTeX documents), but I have 30+ pattern (eg. \al\beta\del\b\i\sig) to replace and the number increases in time. (Thanks google doc for its Equation Editor and the recent "improvements" they made)
    – gamebm
    Jan 20, 2013 at 19:50

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

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.

4
  • Thanks a lot for the explanation, now I think I understand better. In my real problem, I actually have more than 30+ patterns, so I shall just simply repeat the whole script again.
    – gamebm
    Jan 20, 2013 at 19:32
  • 1
    That may work well enough. Alternatively, you could use Perl or Python instead of sed and use trailing context in the patterns so that the second backslash in `\right` is not part of the first match and the repeat continues where you want it to. Jan 20, 2013 at 19:34
  • I have to study those things, I know that sed is not good at manipulating multi-line document, therefore I cannot properly treat them, for example, once spot a \\ (double backslach) I need to add a \n (endline) and an additional & at the beginning of the next line if they are not there already. (\alpha\\\beta goes to \alpha\n&\beta) This is for proper alignment of equations array. I know perl should easily handle this, but it seems to much for sed.
    – gamebm
    Jan 20, 2013 at 20:46
  • I'd say sed handles multi-line documents (files) fine; it doesn't handle multi-line patterns anywhere near as easily. I'm not clear that you have to worry about multi-line patterns in your work, though. You might need to consider whether your code handles \ri at the end of a line, for example. I've not reviewed your patterns for that. Jan 20, 2013 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.