Sponsored Content
Full Discussion: Binary search and replace
Top Forums UNIX for Dummies Questions & Answers Binary search and replace Post 302903454 by wisecracker on Tuesday 27th of May 2014 05:33:08 PM
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...
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
shells(4)							   File Formats 							 shells(4)

NAME
shells - shell database SYNOPSIS
/etc/shells DESCRIPTION
The shells file contains a list of the shells on the system. Applications use this file to determine whether a shell is valid. See getuser- shell(3C). For each shell a single line should be present, consisting of the shell's path, relative to root. A hash mark (#) indicates the beginning of a comment; subsequent characters up to the end of the line are not interpreted by the routines which search the file. Blank lines are also ignored. The following default shells are used by utilities: /bin/bash, /bin/csh, /bin/jsh, /bin/ksh, /bin/ksh93, /bin/pfcsh, /bin/pfksh, /bin/pfsh, /bin/sh, /bin/tcsh, /bin/zsh, /sbin/jsh, /sbin/sh, /usr/bin/bash, /usr/bin/csh, /usr/bin/jsh, /usr/bin/ksh, /usr/bin/ksh93, /usr/bin/pfcsh, /usr/bin/pfksh, /usr/bin/pfsh, and /usr/bin/sh, /usr/bin/tcsh, /usr/bin/zsh, and /usr/sfw/bin/zsh. /etc/shells overrides the default list. Invalid shells in /etc/shells could cause unexpected behavior, such as being unable to log in by way of ftp(1). FILES
/etc/shells list of shells on system SEE ALSO
vipw(1B), ftpd(1M), sendmail(1M), getusershell(3C), aliases(4) SunOS 5.11 20 Nov 2007 shells(4)
All times are GMT -4. The time now is 10:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy