Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex String Replacement Using sed

Tags:

sed

I'm having some trouble getting sed to do a find/replace of some hex characters. I want to replace all instances within a file of the following hexadecimal string:

0x0D4D5348

with the following hexadecimal string:

0x0D0A4D5348

How can I do that?

EDIT: I'm trying to do a hex find/replace. The input file does not have the literal value of "0x0D4D5348" in it, but it does have the ASCII representation of that in it.

like image 683
Carter Avatar asked Oct 13 '11 21:10

Carter


3 Answers

GNU sed v3.02.80, GNU sed v1.03, and HHsed v1.5 by Howard Helman all support the notation \xNN, where "NN" are two valid hex numbers, 00-FF.

Here is how to replace a HEX sequence in your binary file:

$ sed 's/\x0D\x4D\x53\x48/\x0D\x0A\x4D\x53\x48/g' file > temp; rm file; mv temp file

As @sputnik pointed out, you can use sed's in place functionality. One caveat though, if you use it on OS/X, you'd have to add an empty set of quotes:

$ sed '' 's/\x0D\x4D\x53\x48/\x0D\x0A\x4D\x53\x48/g' file

As sed in place on OS/X takes a parameter to indicate what extension to add to the file name when making a backup, since it does create a temp file first. But then.. OS/X's sed doesn't support \x.

like image 132
tolitius Avatar answered Nov 12 '22 04:11

tolitius


This worked for me on Linux and OSX.

Replacing in-place:

sed -i '.bk' 's'/`printf "\x03"`'/foo/g' index.html

(See @Ernest's comment in the answer by @tolitius)

like image 38
Andrew E Avatar answered Nov 12 '22 04:11

Andrew E


In OS/X system's Bash, You can use command like this:

# this command will crate a variable named a which contains '\r\n' in it
a=`echo -e "hello\r\nworld\r\nthe third line\r\n"`

echo "$a" | sed $'s/\r//g' | od -c

and now you can see the output characters :

0000000    h   e   l   l   o  \n   w   o   r   l   d  \n   t   h   e
0000020    t   h   i   r   d       l   i   n   e  \n
0000033

You should notice the difference between 's/\r//g' and $'s/\r//g'. Based on the above practices, you can use command like this to replace hex String

echo "$a" | sed $'s/\x0d//g'  | od -c
like image 4
user3033075 Avatar answered Nov 12 '22 04:11

user3033075