How do I replace backspace characters (\b) using sed?
15,494
Solution 1
sed interprets \b
as a word boundary. I got this to work in perl like so:
echo -e "1234\b\b\b56" | perl -pe '$b="\b";s/$b//g'
Solution 2
You can use the hexadecimal value for backspace:
echo -e "1234\b\b\b56" | sed 's/\x08\{3\}//'
You also need to escape the braces.
Solution 3
You can use tr
:
echo -e "1234\b\b\b56" | tr -d '\b'
123456
If you want to delete three consecutive backspaces, you can use Perl:
echo -e "1234\b\b\b56" | perl -pe 's/(\010){3}//'
Solution 4
Note if you want to remove the characters being deleted also, have a look at ansi2html.sh which contains processing like:
printf "12..\b\b34\n" | sed ':s; s#[^\x08]\x08##g; t s'
Solution 5
With sed:
echo "123\b\b\b5" | sed 's/[\b]\{3\}//g'
You have to escape the {
and }
in the {3}
, and also treat the \b
special by using a character class.
[birryree@lilun ~]$ echo "123\b\b\b5" | sed 's/[\b]\{3\}//g'
1235
Comments
-
Thiago Padilha almost 2 years
I want to delete a fixed number of some backspace characters ocurrences ( \b ) from stdin. So far I have tried this:
echo -e "1234\b\b\b56" | sed 's/\b{3}//'
But it doesn't work. How can I achieve this using sed or some other unix shell tool?