sed non ascii value remove

Hi All,

i am using the below perl command to remove the non ascii value,it is working fine.
we need to find the similar solution using the sed command. but i tried it is not working and getting the error.

perl -pe 's/[[:^ascii:]]//g'  test.txt

sed is not working.

sed -i 's/[^[:ascii:]]//g' test.txt 

Error:

sed: -e expression #1, char 17: Invalid character class name

ascii is not found in the wctype character classes, nor mentioned in e.g. man regex .

What characters do you want to remove? Strange locales'? Be aware that control chars 0x00 - 0x1F (including e.g. <TAB> ) are in the ascii set as well...

Classes like [:ascii:] and [:word:] are non-POSIX additional classes that can be found in perl, but are not recognised by standard sed .

Instead of [:ascii:] , with GNU sed you could try a regex character range:

sed 's/[^\x00-\x7F]//g'  test.txt

To remove non-printable and non-"standard ascii" characters as RudiC suggests, you can try:

sed 's/[^\x20-\x7E]//g'  test.txt

--
Note:
I presume [[:^ascii:]] is a typo and you meant to write [^[:ascii:]]

1 Like

@Scrutinizer @RudiC thanks for quick respone.

i tried below one and getting the below error.

sed 's/[^\x00-\x7F]//g'

sed: -e expression #1, char 17: Invalid collation character

What is your OS and version.
Can you show the exact command?

FYI..Red Hat Enterprise Linux 7
VERSION_ID="7.7"

And what is the command?

sed 's/[^\x00-\x7F]//g' test.txt 

What's your locale , esp. the LC_COLLATE variable?

Try that command prefixed with LC_ALL=C .

1 Like

@Rudic Thanks a lot..it is working fine

one clarification.always it will remove the non printable characters?

No. It will remove characters that are not in the "ASCII range" from 0x00 up to 0x7F . ASCII control chars (non- printable, incl. white space) will NOT be removed. It will remove characters above ASCII, starting with 0x80 (= 128), including "extended ASCII" or any other character set / encoding like UTF-8, on which your locale setting may depend.

You may want to consider starting over, making up your mind which chars you need, and which you don't, and rephrase your specification. Do you have examples of "target" chars ?

@Rudic..Thanks a lot ...:slight_smile: