As others have already mentioned, \x1
is the ASCII escape sequence for the character Control-A as represented by ^A
in most displays. Rather than having to deal with [ranges of] specific ASCII escape sequences, consider using a POSIX character class:
sed 's/[[:cntrl:]]/|/g'
to just change all control-characters in each input line to |
s so you don't have to worry about which control characters are present or what their associated ASCII escape values are.
For example:
$ printf '\x01foo\x02bar\x03\n' | cat -A
^Afoo^Bbar^C$
$ printf '\x01foo\x02bar\x03\n' | sed 's/[[:cntrl:]]/|/g' | cat -A
|foo|bar|$
See the POSIX regexp spec for more info on bracket expressions, [...]
, and the character class [:cntrl:]
.
You don't need the \
before |
btw, there's nothing special about |
in sed replacement text, nor even in a Basic Regular Expression as sed uses by default.
\x1
matches it. ^B is printed for ASCII code 2 so you'd need\x02
.sed -n 'l0' file
and the such sequences are represented by\nnn
e.gprintf '\x01foo\x02bar\x03baz' | sed -n l0
will output\001foo\002bar\003baz$
. Thus to replace the first control sequence usingprintf '\x01foo\x02bar\x03baz' | sed 's/\o001/X/'
will outputXfoobarbaz
.