Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

search and replace (in-place) strings in binary

by Laguna (Initiate)
on Jul 16, 2020 at 16:01 UTC ( #11119409=perlquestion: print w/replies, xml ) Need Help??

Laguna has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I need search and replace (in-place) textual strings in binary file (mac address, case-sensitive), use terminal command. Linux, perl 5 installed (v5.22.1) What would be appropriate perl command for this? Is this correct: perl -i.bak -pe 's/search-this-string/replace-with-this-string/g' file.bin

Replies are listed 'Best First'.
Re: search and replace (in-place) strings in binary
by choroba (Archbishop) on Jul 16, 2020 at 16:37 UTC
    Might be.

    What's the actual string you want to replace? If it contains some special characters, they might need escaping.

    Also, what's the format of the files? Some binary formats might contain CRC checks which would be invalidated by changing some bytes. Also, if the binary format is fixed length or records the lengths of dynamic parts, you might need to pad the inserted string or update the recorded length.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: search and replace (in-place) strings in binary
by ikegami (Pope) on Jul 16, 2020 at 16:46 UTC

    That technically doesn't open the file in binary mode, but there's no difference between text mode and binary mode (by default) in Linux.

    But you are reading the file a line at a time. What's a line in a binary file? That makes no sense. Perhaps you should make it so the entire file is one line by using -0777. (That's a zero.)

Re: search and replace (in-place) strings in binary
by LanX (Cardinal) on Jul 16, 2020 at 16:37 UTC
    please define "textual strings in binary file"

    best by pasting an example

    > Is this correct: perl -i.bak -pe s/search-this-string/replace-with-this-string/g file.bin

    so what happened?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: search and replace (in-place) strings in binary
by Marshall (Canon) on Jul 18, 2020 at 09:25 UTC
    I would like to see a detailed example of the exact situation.
    The reason is that I'm not sure exactly what the OP has in this binary file.
    See MAC address wiki for gory details

    One common form of human readable text is like: 01:23:45:67:89:AB. In a binary form, that is 6, eight bit bytes (2 hex digits each) or 48 bits or 3, 16 bit unsigned integers. If that human form is stored as a string for easy output to humans, that's 17 bytes of ASCII plus probably a trailing zero. A binary file would typically just store the 48 bits (not a string).

    You can use regex to replace one binary string with another binary string. However, I am not sure that is best here because we have almost no application details. I suspect that more code than just one regex will be required for a good UI - could be wrong about that - perhaps depends upon what is meant by "good UI"! There are also other possibilities in the menagerie of Perl options.

    I hope the OP can show at least a partial hex dump of the file and then explain what he wants to replace with what.
    And comment about potential CRC characters is spot on- probably don't exist, but they might.

    I would be curious to know a bit higher level of the app like why a low level binary edit is needed?

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11119409]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-02-24 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?