Search code examples
sedspecial-charactersbibtex

How to replace {\'{\i} by {\'i} with sed


I'm using Papers3 to export a Bibtex library, but there are errors with some of the accents. Many come out missing a curly brace, and therefore give a compilation error when I run bibtex. Here is an example of an error in the bib file:

author = {Combi, J A and Rib{\'o}, M and Mart{\'{\i}, J and Chaty, S.},

I want to replace all instances of these in my file (agn.bib), using something like:

sed "s/{\'{\i}/{\'i}/g" agn.bib

But that doesn't do anything, and I can't find the answer on Stack Overflow how to do it.


Solution

  • You'll have to escape the backslash twice; once for the shell, once for the sed:

    sed -i "s/{\\\\'{\\\\i}/{\\\\'i}/g" file
    

    since backslash is a metacharacter for both.

    When you say sed "\\\\", due to double quotes, sed actually receives \\, and according to the rules of basic regular expressions, treats the backslash as a literal character.