Hi.
Using the GNU utilities, one could avoid resorting to c:
#!/usr/bin/env bash
# @(#) s1 Demonstrate searching binary file.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C strings head grep
FILE=${1-$(which cut)}
pl " Input data file $FILE is type $(file $FILE)"
pl " Results, strings -- $(wc -l < $FILE) strings found."
pl " Results, strings and first 2 occurrences:"
strings $FILE |
head -2
pl " Results, strings and grep searching for \"bucket\""
strings $FILE |
grep bucket
pl " Results, grep, (expect only a message):"
grep bucket $FILE
pl " Results, grep, every occurrence:"
grep -a bucket $FILE
pl " Results, grep, only first 2 results:"
grep -a -m 2 bucket $FILE
exit 0
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian 5.0.8 (lenny, workstation)
bash GNU bash 3.2.39
strings GNU strings (GNU Binutils for Debian) 2.18.0.20080103
head (GNU coreutils) 6.10
grep GNU grep 2.5.3
-----
Input data file /usr/bin/cut is type /usr/bin/cut: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, stripped
-----
Results, strings -- 130 strings found.
-----
Results, strings and first 2 occurrences:
/lib64/ld-linux-x86-64.so.2
__gmon_start__
-----
Results, strings and grep searching for "bucket"
# buckets: %lu
max bucket length: %lu
# buckets used: %lu (%.2f%%)
-----
Results, grep, (expect only a message):
Binary file /usr/bin/cut matches
-----
Results, grep, every occurrence:
# buckets: %lu
max bucket length: %lu
# buckets used: %lu (%.2f%%)
-----
Results, grep, only first 2 results:
# buckets: %lu
max bucket length: %lu
This uses command cut binary file as the file to be searched.
See man pages for details.
Best wishes ... cheers, drl