1

I have these bytes: E6 2A 1B EF 11 00 00 00 00 00 00 4E 43 DB E8 enter image description here

I need to replace them with these: 64 08 1A EF 11 00 00 00 00 00 DA D8 26 04

When I started to experiment, I've noticed one strange thing.

sed -e 's/\xE6/\x64/g'

This code replaces first E6 with 64 ok

enter image description here

However when I try to change more bytes (2A) causing problem.

sed -e 's/\xE6\x2A/\x64\x08/g' enter image description here

as I understand 2A inserts same code.. How to avoid it? I just need to change 2A with 08. Thanks in advance :)

UPDATED


Now I've stuck on \x26. sed -e 's/\xDB/\x26/g' this code refuses to replace DB to 26, but when I run s/\xDB/\xFF it works. Any ideas? In this way something is wrong with 26. I have tried [\x26], not helped here.

OK. s/\xDB/\&/g' seems to be working :)

Dewagg
  • 145
  • 10

1 Answers1

2

\x2a is *, which is special in regex :

$ sed 's/a*/b/' <<<'aaa'
b
$ sed 's/a\x2a/b/' <<<'aaa'
b

You may use bracket expression in regex to cancel the special properties of characters, but I see it doesn't work well with all characters with my GNU sed:

$ sed 's/\xE6[\x2A]/OK/' <<<$'I am \xE6\x2A'
I am OK
$ sed 's/[\xE6][\x2A]/OK/' <<<$'I am \xE6\x2A'
I am �*

That's because \xE6] is probably some invalid UTF character. Remember to use C locale:

$ LC_ALL=C sed 's/[\xE6][\x2A]/OK/' <<<$'I am \xE6\x2A'
I am OK

Remember that \1 \2 etc. and & are special in replacement part too. Read Escape a string for a sed replace pattern - but you will need to escape \xXX sequences instead of each character (or convert characters to the actual bytes first, why work with all the \xXX sequences?). Or pick another tool.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • now I've stuck on \x26. `sed -e 's/\xDB/\x26/g'` this code refuses to replace DB to 26, but when I run `s/\xDB/\xFF` it works. Any ideas? – Dewagg Feb 21 '21 at 13:55
  • `Remember that \1 \2 etc. and & are special in replacement part too` - did you? And read https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern - did you? – KamilCuk Feb 21 '21 at 15:34