Binary search and replace


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Binary search and replace
# 1  
Old 05-27-2014
Scissors Binary search and replace

Hello again. I have two problems - is it possible to solve them?
1. I want to replace a few bytes after specific hex-string.
i.e.: I want to replace two bytes after AA AB AC:
Code:
AA AB AC 00 00 AA AA AA

so the expected result should be:
Code:
AA AB AC FF FF AA AA AA

2. I want to replace three bytes before specific hex-string.
i.e.: replace two bytes before 00 00 with FF FF:
Code:
AA AB AC 00 00 AA AA AA

result:
Code:
AA FF FF 00 00 AA AA AA

Everything above is pseudo hexcode. Also I attached real binary files to this post. Since this question was posted at "newbies" section, I'm expecting all possible solutions. Thank you

Last edited by useretail; 05-27-2014 at 05:01 PM..
# 2  
Old 05-27-2014
Firstly, give us a clue as to your OS, shell, terminal, etc...

Secondly this is not a binary file.
They are ASCII representations of hexadecimal pairs separated by space delimiters.

Thirdly how large will your (pseudo)-binary be?

Fourthly will there always be two "00" pairs?

Lastly in number 2 do you want three "FF" pair values or two as shown in your results?

Whichever the case then this might be of help as a starter:-

Another Building Block, Binary File Manipulation...
# 3  
Old 05-27-2014
And, to add another question,
is the data in the file formatted like your example, or are you doing something to represent the data in this manner (like an od command)?
# 4  
Old 05-27-2014
OS: POSIX-compliant
shell: sh/bash
terminal: rxvt (does it really matter?)

filesize: i don't know.. from 8 bytes to 8 terabytes

will there always be two "00" pairs: have no idea

do you want three "FF" pair values: no, its just an example

*updated 1st post
**btw, i know how to replace using sed, but i have no clue how to replace before or after some hexstring

---------- Post updated at 03:27 PM ---------- Previous update was at 03:06 PM ----------

i don't think that binary manipulations using hexdump (in this case) are wise:
Quote:
$man hexdump
The hexdump utility is a filter which displays the specified files ...
I understand that in linux everything goes, but let's face it - hexdump wasn't made for that purpose.

Last edited by useretail; 05-27-2014 at 05:16 PM..
# 5  
Old 05-27-2014
An example for the first part longhand using OSX 10.7.5, default bash terminal...

Altered the binary file to show it works...
Code:
#!/bin/bash
# bin_change.sh
# This will be SSLLOOWW with large files...
ifs_str="$IFS"
IFS=" "
> /tmp/bin
> /tmp/newbin
printf "\xBB\xCC\xAA\xAA\xAA\xAB\xAC\x00\x00\xAA\xAA\xAA" >> /tmp/bin
printf "\xBB\xCC\xAA\xAA\xAA\xAB\xAC\x00\x00\xAA\xAA\xAA" >> /tmp/bin
printf "\xBB\xCC\xAA\xAA\xAA\xAB\xAC\x00\x00\xAA\xAA\xAA" >> /tmp/bin
printf "\xBB\xCC\xAA\xAA\xAA\xAB\xAC\x00\x00\xAA\xAA\xAA" >> /tmp/bin
# Check existence of 48 bytes of example binary...
hexdump -C /tmp/bin
n=0
bin_array=(`hexdump -v -e '1/1 "%02X "' /tmp/bin`)
echo ""
# 48 is the /tmp/bin filelength.
while [ $n -lt 48 ]
do
	if [ "${bin_array[$n]}${bin_array[$((n+1))]}${bin_array[$((n+2))]}" == "AAABAC" ] && [ "${bin_array[$((n+3))]}${bin_array[$((n+4))]}" == "0000" ]
	then
		printf "\x${bin_array[$n]}\x${bin_array[$((n+1))]}\x${bin_array[$((n+2))]}\xFF\xFF" >> /tmp/newbin
		n=$((n+5))
	fi
	printf "\x${bin_array[$n]}" >> /tmp/newbin
	n=$((n+1))
done
# Now check new 48 bytes of binary...
hexdump -C /tmp/newbin
echo ""
IFS="$ifs_str"
exit 0

Results:-
Code:
Last login: Tue May 27 22:27:08 on ttys000
AMIGA:barrywalker~> ./bin_change.sh
00000000  bb cc aa aa aa ab ac 00  00 aa aa aa bb cc aa aa  |................|
00000010  aa ab ac 00 00 aa aa aa  bb cc aa aa aa ab ac 00  |................|
00000020  00 aa aa aa bb cc aa aa  aa ab ac 00 00 aa aa aa  |................|
00000030

00000000  bb cc aa aa aa ab ac ff  ff aa aa aa bb cc aa aa  |................|
00000010  aa ab ac ff ff aa aa aa  bb cc aa aa aa ab ac ff  |................|
00000020  ff aa aa aa bb cc aa aa  aa ab ac ff ff aa aa aa  |................|
00000030

AMIGA:barrywalker~> _

HTH, Good Night all...
# 6  
Old 05-27-2014
I'm confused. The title of this thread is Binary search and replace. A binary search is used to search a sorted list. I see no order in the values that you are searching?

Please explain how you are doing anything other than a linear search for five contiguous values and then replacing two of those five matched values.
# 7  
Old 05-28-2014
Hi.

I'd guess that the OP meant Byte or Binary File Search and Replace. We would not expect that everyone knows that there is an efficient procedure for searching ordered collections that goes by that name http://en.wikipedia.org/wiki/Binary_search_algorithm ... cheers, drl

Last edited by drl; 05-28-2014 at 12:52 PM..
This User Gave Thanks to drl For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C program to read a binary file and search for a string?

Hi, I am not a C programmer. The only C exposure I have is reading and completing the exercises from the C (ANSI C ) Programming Language book:o At the moment, I am using the UNIX strings command to extract information for a binary file and grepping for a particular string and the value... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

How to search specific object in binary file?

I have a very important question: I have to find a table 'XXTO_AR_TABLE' in a folder present in server and in this folder these types files are present: .rdf, jar file, java class file etc. These are binary files. I want to get name of these files where my table named 'XXTO_AR_TABLE' is... (2 Replies)
Discussion started by: Vikash163
2 Replies

3. Programming

Binary search tree questions. Please help =)

I have some questions about certain placement of child nodes since I'm just learning BSTs and it's quite confusing even after reading some sources and doing some online insertion applets. Let's say I want to add nodes 5,7,3,4 to an empty basic BST. ... (1 Reply)
Discussion started by: Jill Ceke
1 Replies

4. Shell Programming and Scripting

Regex and backreference to replace in binary file

Hello to all, I have this sed script that replaces hex strins within a binary file. As you can see, I want to replace all bytes 4X with 2X (where X could take values 0 to F). sed -e 's/\x40/\x20/g' -e 's/\x41/\x21/g' -e 's/\x42/\x22/g' -e 's/\x43/\x23/g' -e 's/\x44/\x24/g' -e... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

5. Programming

Binary Search Tree Search problem

I am writing code for a binary search tree search and when I compile it i am getting strange errors such as, " /tmp/ccJ4X8Xu.o: In function `btree::btree()': project1.cpp:(.text+0x0): multiple definition of `btree::btree()' " What does that mean exactly? tree.h #ifndef TREE_H #define... (1 Reply)
Discussion started by: meredith1990
1 Replies

6. Shell Programming and Scripting

search 32 bit binary for the 1's and return a 1 to the placeholder variable

Folks , I have a korn shell script that i have written for assembly, the variable that is a final result is returning hexadecimal, now the value is to be converted to binary and return the place holder in the binary that has a 1 in its place and send it to a variable assigned for the... (0 Replies)
Discussion started by: venu
0 Replies

7. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

replace string in binary file

Dear all, How can replace special string with another string in binary file? Thanks&Best Regards (2 Replies)
Discussion started by: sg1207
2 Replies
Login or Register to Ask a Question