1

binary file file.f1

which has String abc I want to overwrite it with adcd

perl -pi -e s/abc/abcd/ file.f1

works but it inserts it rather than overwriting it, which causes error for the program which uses it

I'm not sure how will I be able to do that without making things more complex, I'd prefer if it used tools like sed, grep, python, perl one liners which are available by default on UNIX system

I'm not very experienced user and am very new to these tools

edit- hope its clear now

data inside bin file is like

[abc def xyz]

when doing perl -pi -e s/abc/abcd/ file.f1

it becomes [abcd def xyz]

what i want is to overwrite it with a extra [space] so it becomes

[abcd ef xyz]

R347
  • 17
  • 4
  • 1
    It depends on what type of binary file it is. Binary files are usually quite rigid. There is no general purpose answer. Adding or deleting bytes is probably a non-starter. – John Kugelman Nov 13 '19 at 19:36
  • You do not want to change bytes in a binary file. At the very minimum you do not want to change the length of the file. Binary files often encode lengths, type, etc.. in the bytes of the file. Changing `abc` to `abcd` where `'d'` was originally used as the number of elements in a 2137 element array -- is going to cause real problems with the code that reads that file. Before you can make any change you have to know how the file is being used. – David C. Rankin Nov 13 '19 at 19:46
  • i can use gui hex editor apps which have 'overwrite mode' to do the same, but i wanted to create a script to automate the same process – R347 Nov 13 '19 at 19:51
  • @DavidRankin-ReinstateMonica i understand, since i have already modified the file in hex editors, i know that if abcd is written over it would result in overlapping next String which is fine as values are seperated by spaces, lets say there is [abc def] so i can just add 'abcd+[space]' which would result in [abcd ef] and values after 'def' dont really matter for the program – R347 Nov 13 '19 at 20:01
  • Any chance you can share sample input file (may be few few lines in hex format) ? It will make it easier to offer solutions. – dash-o Nov 14 '19 at 04:48

2 Answers2

0

You are trying to patch a binary file. Perl RE are not set for this type of process. While they will work MOST time, specific sequences may trick the RE engine, which assume the file to be text. Use with care.

To get replacement, make the source string match the length of the target string

perl -pi -e 's/abc./abcd/' file.f1

Perl will replace the first 4 byte string that starts with abc with abcd. If you suspect that the 4th character may be special (e.g. new line, or similar), use the single line mode. It will allow '.' to match ANY character.

perl -pi -e 's/abc./abcd/s' file.f1
dash-o
  • 13,723
  • 1
  • 10
  • 37
  • is there a way to specify more than one character – R347 Nov 13 '19 at 20:43
  • For Perl RE, each '.' in the source will match a single character. Make sure to extend the replacement string by the same length. – dash-o Nov 14 '19 at 04:47
-3
perl -pi -e 's/blue/red/g' $file_name

The g at the end is required. Another tool to use would be sed for these kinds of tasks.

Another post about using perl

zinnadean
  • 33
  • 1
  • 6