Including extended ASCII with regex and sed

151 Views Asked by At

I want to select all characters, including extended ASCII inside a group of regex using sed.

  • Next works:
echo -e "TO BE REMOVED. THIS NOT. TO BE REMOVED" | sed -E 's/(TO BE REMOVED)(.{12})(.*)/IT WAS REMOVED\2/g'

And I get:

IT WAS REMOVED. THIS NOT.
  • But, if I insert an extended ASCII character inside one group, it doesn't work:
echo -e "TO BE REMOVED. THIS\xa5NOT. TO BE REMOVED" | sed -E 's/(TO BE REMOVED)(.{12})(.*)/IT WAS REMOVED\2/g'
  • Trying to include extended ascii characters inside the sed regex, I can do the following:
echo -e "HI FRIEND, \xa5 ARE YOU?" | sed -E 's/\xa5/HOW/g'

This becomes:

HI FRIEND, HOW ARE YOU?
  • But if I try to include all extended ascii characters with squared brackets, it doesn't work:
echo -e "HI FRIEND, \xa5 ARE YOU?" | sed -E 's/[\xa0-\xfe]/HOW/g'

I get the input

HI FRIEND,  ARE YOU?
1

There are 1 best solutions below

0
potong On

This might work for you (GNU sed):

sed 's/[^[:print:]]/HOW/g' file

See here for more information.