Hex String Replacement Using sed

后端 未结 3 1473
渐次进展
渐次进展 2020-12-14 02:13

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:

相关标签:
3条回答
  • 2020-12-14 02:19

    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.

    0 讨论(0)
  • 2020-12-14 02:20

    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
    
    0 讨论(0)
  • 2020-12-14 02:37

    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)

    0 讨论(0)
提交回复
热议问题