1

Am running BASH and UNIX utilities on Windows 7.

Have a file that contains a vertical tab. The binary symbol is 0x0B. The octal symbol is 013. I need to replace the symbol with a blank space.

Have tried this sed approach but it fails:

sed -e 's/'$(echo "octal-value")'/replace-word/g'

Specifically:

sed -e 's/'$(echo "\013")'/ /g'

Update:

Following this advice I use GNU sed and this approach:

sed -i 's:\0x0B: :g' file

but the stubborn vertical tab is still in the file.


What is the correct way to replace a non-printable character with a printable character?

Community
  • 1
  • 1
Jay Gray
  • 1,706
  • 2
  • 20
  • 36

2 Answers2

3

Sed should recognise special characters:

sed -e 's/\x0b/ /g'
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Correct, but why do you pass `-e`? – hek2mgl Apr 07 '15 at 14:22
  • @hek2mgl: Because some versions of sed don't work without it. – choroba Apr 07 '15 at 14:22
  • Wanted to hear that! (Could imagine you are simply doing it without a reason). Can you name versions which does not support omitting `-e` ? – hek2mgl Apr 07 '15 at 14:24
  • @hek2mgl: Some non-GNU versions, IIRC. – choroba Apr 07 '15 at 14:26
  • @choroba Tried your advice (with the -e) but 0x0B is still in the output file. For the LHS I have tried \0x0B and \x0B - but the char will not die. Will try another version of sed. – Jay Gray Apr 07 '15 at 14:43
  • @choroba Turns out I was not using GNU sed. When I switched to GNU sed your solution worked (without -e). Also, per your advice, I use \x0B rather than \0x0B. TY. – Jay Gray Apr 07 '15 at 14:56
0

In answer to why the -e? If you use more than one sed expression, then each one must be preceded by the -e. So, for example:

echo foo bar bas zer | sed -e 's/zer/oh my/g' -e 's/bas/baz/'

would result in:

foo bar baz oh my

thus performing 2 different sed changes ('scripts) with only a single invocation. See sed man pages for more details.

(the above example is, obviously, contrived. I, however, have seen a sed command in a script with 78 individual -e 'scripts'!)

If you only have one 'script', then the -e is optional, obviously.

RustyCar
  • 401
  • 1
  • 4
  • 9