How to find and replace all percent, plus, and pipe signs?

sed

Solution

Test script:

#!/bin/bash

cat << 'EOF' > testfile.txt
1+2+3=6
12 is 50% of 24
The pipe character '|' looks like a vertical line.
EOF

sed -i -r 's/%/\\textpercent /g;s/[+]/\\textplus /g;s/[|]/\\textbar /g' testfile.txt

cat testfile.txt

Output:

1\textplus 2\textplus 3=6
12 is 50\textpercent  of 24
The pipe character '\textbar ' looks like a vertical line.

This was already suggested in a similar way by @tripleee, and I see no reason why it should not work. As you can see, my platform uses the very same version of GNU sed as yours. The only difference to @tripleee's version is that I use the extended regex mode, so I have to either escape the pipe and the plus or put it into a character class with `[]`.

Problem

I have a document containing many percent, plus, and pipe signs. I want to replace them with a code, for use in TeX. - `%` becomes `\textpercent`. - `+` becomes `\textplus`. - `|` becomes `\textbar`. This is the code I am using, but it does not work: ``` sed -i "s/\%/\\\textpercent /g" ./file.txt sed -i "s/|/\\\textbar /g" ./file.txt sed -i "s/\+/\\\textplus /g" ./file.txt ``` How can I replace these symbols with this code?

Original source