E B
E B

Reputation: 1073

use sed to replace a range of hex values

I want to replace the series of hex characters from x'00 to x'40' to spaces in my file. I would not want to write a sed -e 's/\x00//g' for each one of them.. is there a more efficient way of doing this?

Upvotes: 4

Views: 9878

Answers (3)

sbin_bash
sbin_bash

Reputation: 1

this:

LC_ALL=C sed 's/[\x00-\x40]/ /g'

or rough:

(LC_ALL=C; sed 's/[\x00-\x40]/ /g')

Test:

alias hxr="perl -ne'for(split){print chr hex}'"

printf '%02X ' `seq 65` | hxr | hd

printf '%02X ' `seq 65` | hxr | LC_ALL=C sed 's/[\x00-\x40]/ /g'; echo

Upvotes: 0

William
William

Reputation: 4935

This may do it for you, depending on your environment and version of sed. (It avoids messing with tab, but there's no reason to avoid space since you're just replacing it with another space.)

LANG='' sed 's/[\x00-\x08\x0A-\x40]/ /g'

This clears LANG for the duration of the sed command. That can solve some problems if you're environment is UTF8, for example.

Upvotes: 4

John Zwinck
John Zwinck

Reputation: 249153

Use tr instead of sed. First you must convert your character codes from hex to octal. Let's say you want 0 to 77 octal. Here that is:

tr '\0-\77' ' ' < your-input-file

Upvotes: 1

Related Questions