Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all bullets with spaces

Tags:

bash

How can I replace bullets (octal value: 225, hexadecimal value: 95) with spaces?
I tried with the following commands:

echo '•test' | tr '\225' ' '

echo '•test' | awk '{gsub(/\225/," ");print $0}'

echo '•test' | sed 's/\o225/ /g'

echo '•test' | LANG='' sed 's/\o225/ /g'

echo '•test' | sed 's/\x95/ /g'


The above commands does not work.

like image 796
user2874781 Avatar asked Dec 25 '22 06:12

user2874781


1 Answers

Let's look at why your current efforts are failing:

$ echo '•test' | hexdump -C
00000000  e2 80 a2 74 65 73 74 0a                           |...test.|
00000008

These bullets are actually three bytes -- e2 80 a2, not a single 0x95.

A corrected sed expression works fine:

echo '•test' | sed -e 's/•/ /g'

...or (using bash-extended syntax not available in /bin/sh)...

echo '•test' | sed -e  $'s@\xe2\x80\xa2@ @g'

...or (using bash-builtin replacement functionality):

s='•test'             # original string in s
orig='•'              # item to replace
new=' '               # thing to replace it with
s2=${s//"$orig"/$new} # result in s2

...or (using GNU sed extensions, per @anubhava)...

echo '•test' | sed 's@\xe2\x80\xa2@ @g'
like image 107
Charles Duffy Avatar answered Jan 08 '23 09:01

Charles Duffy