Replace HEX characters (\x20) from string with ASCII space

hex to ascii
ascii table
x20 ascii

I'm running a shell command which returns raw string that is a combination of ASCII and Hex chars. The character it embeds is space-chars in hex. Below is the output:

KINGSTON\x20SV100S2
ST380011A\x20\x20\x20\x20\x20\x20\x20
Maxtor\x206L300S0\x20\x20

How can I replace all the \x20 into single ASCII space character?

Expected output:

KINGSTON SV100S2
ST380011A
Maxtor 6L300S0

P.S the string is stored in a bash variable, hence I would prefer a solution that doesn't input file.


To only replace \x20 and nothing else use sed:

sed 's/\\x20/ /g' <<< "$output"

or

yourCommand | sed 's/\\x20/ /g'

To replace all escape sequences use echo -n (as kvantour already pointed out) or even better, the more portable printf %b which can also assign directly to variables without using $():

printf -v output %b "$output"

Replace HEX characters (\x20) from string with ASCII space, Replace HEX characters (\x20) from string with ASCII space. hex to ascii ascii table x20 ascii. I'm running a shell command which returns raw string that is a  if hex value for # is 000D. then use: REPLACE ALL OCCURANCE OF cl_abap_char_utilities=>cr_lf(1) IN string WITH space. if hex value is 000A. then use: REPLACE ALL OCCURANCE OF cl_abap_char_utilities=>cr_lf+1(1) IN string WITH space. similary you can check for HORIZONTAL_TAB, VERTICAL_TAB etc. hope this help. Regards


A quick way would be to use echo -e

$ echo -e "KINGSTON\x20SV100S2"
KINGSTON SV100S2

-e enable interpretation of backslash escapes

Or just use printf

$ printf "KINGSTON\x20SV100S2"
KINGSTON SV100S2

Escape all characters in a string using both Unicode and , using hex and/or Unicode escape sequences (whichever are shortest). return str.replace(/[\s\S]/g, function(character) { '\\35' 30 '\u001e' '\\x1e' '\\36' 31 '\u001f' '\\x1f' '\\37' 32 ' ' '\\x20' '\\40' 33 '!' '\\x21' '\\41' 34 The following function fixes this by matching all non-ASCII characters after splitting the string in a "unicode-safe"  ^ not \x20 hex code for space character - to \x7e hex code for ~ (tilde) character All the ascii printing characters fall between these two. This statement matches non ascii characters as well as ascii control (non printing) characters such as bell, tab, null and others. Look at . man ascii on a unix system to see which characters it matches.


printf is more portable than echo -e. You need to use %b format. As per help printf:

%b expand backslash escape sequences in the corresponding argument

Define a utility function:

expbs() { printf '%b\n' "$@"; }

Then use it as:

expbs 'KINGSTON\x20SV100S2'
KINGSTON SV100S2

expbs 'ST380011A\x20\x20\x20\x20\x20\x20\x20'
ST380011A

expbs 'Maxtor\x206L300S0\x20\x20'
Maxtor 6L300S0

JavaSript: Remove all non printable and all non ASCII characters , Character 0x20 (or 32 in decimal) is the space character ' ' and character 0x7E (​or… used the following to filter all other characters out of our string in JavaScript. expression using the hex values of the two characters as follows: 1. printable_ASCII_only_string = input_string.replace(/[^\x20-\x7E]+/g, "" );  This handles characters one by one and would still use one space per character replaced. Your regular expression should just replace consecutive non-ASCII characters with a space: re.sub(r'[^\x00-\x7F]+',' ', text) Note the + there.


You can try Perl also

$ perl -e ' BEGIN { print "KINGSTON\x20SV100S2","\n" } '
KINGSTON SV100S2
$

or

you can export it to environment

$ export a="KINGSTON\x20SV100S2"
$ perl -e ' BEGIN { $x=$ENV{a}; $x=~s/\\x(\d{2})/chr(hex($1))/ge; print $x,"\n" } '
KINGSTON SV100S2
$

Strip control codes and extended characters from a string, a string with control codes stripped (but extended characters not stripped); a string with In ASCII, the control codes have decimal codes 0 through to 31 and 127. std::string noControls( " " ) ;//creating space for writeln "without ctrl chars: ", replace(.str, RE/\p{Cc}/, ZLS) (regsub #/[^\x20-\x7F]+/ "" str))  I prevented this from happening by using the %XLATE built in function in RPGLE/RPG IV to replace all occurrences of the double quote with a space. But this week they sent a right double quotation mark ( ”), which were translated to some strange hexadecimal characters by the ASCII to EBCDIC translation table.


Practical Programming in Tcl/Tk, Any printing characters, not including space characters. Valid hexadecimal dieits. uses string map to replace fancy quotes and hyphens produced by Microsoft Word Example 4-4 Mapping Microsoft World special characters to ASCII. proc lf puts $output [string map { \223 \224 format "%#x" 20 => 0x14 format "%I08x"  Unicode characters; Windows ALT codes; HTML char codes; ASCII of 0; ASCII of 'A' ASCII of enter; ASCII of space; ASCII,Hex,Dec,Bin,Base64 converter; Hex,Dec,Bin converter with bit toggle; ASCII to binary converter; ASCII to hex converter; Binary to ASCII converter; Hex to ASCII converter


Strip Non-Printable ASCII Characters (SAS), The good news is they are easy to filter out or to replace. Say you don't want characters before hex 20 (which is the space) and after 7E which is the tilde. Here is how to remove both ASCII sets in SAS with a Perl regular This string is encoded in hex. out = prxchange('s/[^\x20-\x7E\x0A\x0D]/X/', -1, in);. I'm running a shell command which returns raw string that is a combination of ASCII and Hex chars. The character it embeds is space-chars in hex. Below is the output: KINGSTON\x20SV100S2 ST380011A\x20\x20\x20\x20\x20\x20\x20 Maxtor\x206L300S0\x20\x20 How can I replace all the \x20 into single ASCII space character? Expected output:


Unicode Character 'SPACE' (U+0020), image of Unicode Character 'SPACE' (U+0020); Browser Do not use this character in domain names. ASCII Punctuation and Symbols HTML Entity (​hex), string.toUpperCase(). string.toLowerCase(). Character.​UnicodeBlock  "00" is one byte, "A0" is the second byte. Unicode strings are double-byte, but the English character set fits into one byte nicely (since we defined the standard :) ). Looked at that way, Unicode is a waste of space - but it does allow for other character sets without having to first "assume" a character set.