473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Perl searching and replacing an HEX pattern

hi,

I require some assistance in understanding how to accomplish replacing a particular hex value in a binary file on aix.

in a binary file I need to find the following combination:

5A 00 10 D3 A9 XX XX XX XX XX XX XX XX XX XX XX XX 0C 5A
XX represents any valid HEX value

then replace this with the following HEX value
5A 00 10 D3 A9 XX XX XX XX XX XX XX XX XX XX XX XX 5A

I am trying to delete the 0C hex value from the binary file when it matches certain conditions:

I tried the following command with spaces and without... it doesn't do anything.

perl -pe 's/\x5A\x00\x10\xD3\xA9\x[0-9a-fA-F]\{24\}\x0C\x5A/\x5A\x00\x10\xD3\xA9\x[0-9a-fA-F]\{24\}\x5A/g' source > destination

I viewed the source file in an HEX editor and it definately has 1 occurance of the HEX sequence
5A 00 10 D3 A9 XX XX XX XX XX XX XX XX XX XX XX XX 0C 5A

I would greatly appreciate any assistance

regards,
Duncan
Mar 8 '08 #1
1 4038
nithinpes
410 Expert 256MB
perl -pe 's/^(5A\s00\s10\sD3\sA9\s([0-9a-fA-F]{2}\s){12})0C\s(5A)/$1$3/g' source > destination

I would like to clarify 3 things in your regex:
1. Use \s for space.
2. You have escaped '{' in \{24\} . This would mean actual {24} and not 24 instances.
3. In [0-9a-fA-F]\{24\} , you are trying to match 24 continuous occurence of the hexadecimal characters. But, there is space after every two characters.
In the above regex, I have grouped characters to make replacement easier with $!,$2 or $#.

I am assuming you are on a linux machine. On Windows, single quotes need to be replaced with doublee quotes. Also, binary files need to be read and written using 'binmode'.
Mar 10 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christopher R. Barry | last post by:
I need to search and replace multiple words in one pass of an input stream or string. For example, given the input: "The quick brown fox jumped over the lazy dog's back" and given the...
77
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for...
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
4
by: Xah Lee | last post by:
20050207 text pattern matching # -*- coding: utf-8 -*- # Python # suppose you want to replace all strings of the form # <img src="some.gif" width="30" height="20"> # to # <img...
2
by: Alex | last post by:
Basically I want to parse some C++ source code and replace some constants. I've mangaed to read in the file and replace some of the things that need replacing. but I'm having a hard time...
4
by: negzero7 | last post by:
Hi everyone, I am trying to teach myself Perl with the aid of some books I have boughten. I'm doing some exercises with regular expressions but am having some trouble, specifically with this...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.