How to replace backspace (\ b) characters with sed?

I want to remove a fixed number of some backspace characters (\ b) from stdin. So far I have tried this:

echo -e "1234\b\b\b56" | sed 's/\b{3}//'

But that will not work. How can I achieve this using sed or some other Unix shell tool?

+3
source share
6 answers

sed interprets \bas the word boundary. I got this to work in perl, for example:

echo -e "1234\b\b\b56" | perl -pe '$b="\b";s/$b//g'
+4
source

You can use the hex value for backspace:

echo -e "1234\b\b\b56" | sed 's/\x08\{3\}//'

You also need to remove the brackets.

+9
source

tr:

echo -e "1234\b\b\b56" | tr -d '\b'
123456

, Perl:

echo -e "1234\b\b\b56" | perl -pe 's/(\010){3}//'
+4

sed:

echo "123\b\b\b5" | sed 's/[\b]\{3\}//g'

{ } {3}, \b .

[birryree@lilun ~]$ echo "123\b\b\b5" | sed 's/[\b]\{3\}//g'
1235
+2

Note that you also want to remove characters to be deleted, see ansi2html.sh , which contains the processing, for example:

printf "12..\b\b34\n" | sed ':s; s#[^\x08]\x08##g; t s'
+2
source

There is no need for Perl!

# version 1
echo -e "1234\b\b\b56" | sed $'s/\b\{3\}//' | od -c

# version 2
bvar="$(printf '%b' '\b')"
echo -e "1234\b\b\b56" | sed 's/'${bvar}'\{3\}//' | od -c
+1
source

Source: https://habr.com/ru/post/1796053/


All Articles