A string in a binary file | Unix Linux Forums | Solaris

  Go Back    


Solaris The Solaris Operating System, usually known simply as Solaris, is a Unix-based operating system introduced by Sun Microsystems. The Solaris OS is now owned by Oracle.

A string in a binary file

Solaris


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-29-2013
q8devilish q8devilish is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 23 March 2014, 5:38 AM EDT
Location: Kuwait
Posts: 31
Thanks: 4
Thanked 0 Times in 0 Posts
A string in a binary file

hi experts,
anyone knows how can i find a string in a multiple binary files in a multiple directories and display the file name containing the string?

i mean i have lots of folders with binary files and i am looking for specific string within one of these files i would like to know the right syntax to use for this kind of search, i know i should run strings or something similar.

OS Solaris 10 sparc
Sponsored Links
    #2  
Old 01-29-2013
Skrynesaver's Avatar
Skrynesaver Skrynesaver is offline Forum Advisor  
Grumpy old geek
 
Join Date: Mar 2011
Last Activity: 31 October 2014, 7:17 AM EDT
Location: Éire
Posts: 825
Thanks: 20
Thanked 185 Times in 181 Posts

Code:
grep  $STRING $(find /root_path -type f)

Or if that returns too many arguments

Code:
for i in $(find /root_path - type f) ; do grep $STRING $i && echo $i;done

Of course you can reduce the search space by adding a -name =\*.o parameter or similar to the find command

Unfortunately Solaris grep does no support the -r flag as far as i know
Sponsored Links
    #3  
Old 01-29-2013
q8devilish q8devilish is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 23 March 2014, 5:38 AM EDT
Location: Kuwait
Posts: 31
Thanks: 4
Thanked 0 Times in 0 Posts
i used the first code and got

Code:
bash: /usr/bin/grep: Arg list too long

i am trying to find a string SRV6000 and another string srv6000, i would like to know which file contain this string most probably will be a binary file in the machine.
    #4  
Old 01-29-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 31 October 2014, 6:36 AM EDT
Location: San Jose, CA, USA
Posts: 4,935
Thanks: 186
Thanked 1,656 Times in 1,405 Posts
Quote:
Originally Posted by q8devilish View Post
hi experts,
anyone knows how can i find a string in a multiple binary files in a multiple directories and display the file name containing the string?

i mean i have lots of folders with binary files and i am looking for specific string within one of these files i would like to know the right syntax to use for this kind of search, i know i should run strings or something similar.

OS Solaris 10 sparc
You are correct in noting that you need to use strings when looking for a string in a binary file. The grep utility is only defined to work on text files. This may not be highly efficient, but it should do what you want. First create the following shell script as a file named $HOME/bin/st_gr_pr :

Code:
#!/bin/ksh
# Usage: st_gr_pr pattern file...
pattern="$1"
shift
for f in "$@"
do      if [ $(strings -a -n ${#pattern} "$f" | grep -ic "$pattern" ) -gt 0 ]
        then    printf "%s\n" "$f"
        fi
done

and make it executable using the command:

Code:
chmod +x $HOME/bin/st_gr_pr

Note that the -i option to the grep command in this script makes searches for alphabetic characters case insensitive. I use ksh, but bash should also work if you want to use it in this script.

Then run the command:

Code:
find . -type f -exec st_gr_pr 'srv6000' {} +

to get a list of all regular files rooted in the current directory that contain srv6000 , SRV6000 , or any of the other six strings with combinations of uppercase and lowercase s, r, and v in that order. Obviously, replace srv6000 if you want to find files containing a different pattern.

Last edited by Don Cragun; 01-29-2013 at 11:29 AM..
The Following User Says Thank You to Don Cragun For This Useful Post:
q8devilish (02-05-2013)
Sponsored Links
    #5  
Old 01-29-2013
GP81 GP81 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 13 January 2014, 7:20 AM EST
Location: Warsaw, Poland
Posts: 65
Thanks: 0
Thanked 14 Times in 13 Posts

Code:
bash: /usr/bin/grep: Arg list too long

Maybe man xargs will help you?
Sponsored Links
    #6  
Old 01-29-2013
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
Solaris 10 may also have ggrep (i.e. GNU grep - depending on the actual Update of Solaris 10) under /usr/sfw/bin.


Code:
$ uname -rsv
SunOS 5.10 Generic_147441-01
$ cat /etc/release
                    Oracle Solaris 10 8/11 s10x_u10wos_17b X86
  Copyright (c) 1983, 2011, Oracle and/or its affiliates. All rights reserved.
                            Assembled 23 August 2011
$ /usr/sfw/bin/ggrep -a -l "GNU" /usr/sfw/bin/ggrep
/usr/sfw/bin/ggrep
$ /usr/sfw/bin/ggrep -a "GNU" /usr/sfw/bin/ggrep
%s (GNU grep) %s
$ /usr/sfw/bin/ggrep "GNU" /usr/sfw/bin/ggrep
Binary file /usr/sfw/bin/ggrep matches

As you can see, you can use the -a option to treat all files as ascii text, and -l to just list the filename that matches. Leave off the -l to print the actual line that matches.

Cheers,
ZB
Sponsored Links
    #7  
Old 02-05-2013
q8devilish q8devilish is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 23 March 2014, 5:38 AM EDT
Location: Kuwait
Posts: 31
Thanks: 4
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Don Cragun View Post
You are correct in noting that you need to use strings when looking for a string in a binary file. The grep utility is only defined to work on text files. This may not be highly efficient, but it should do what you want. First create the following shell script as a file named $HOME/bin/st_gr_pr :

Code:
#!/bin/ksh
# Usage: st_gr_pr pattern file...
pattern="$1"
shift
for f in "$@"
do      if [ $(strings -a -n ${#pattern} "$f" | grep -ic "$pattern" ) -gt 0 ]
        then    printf "%s\n" "$f"
        fi
done

and make it executable using the command:

Code:
chmod +x $HOME/bin/st_gr_pr

Note that the -i option to the grep command in this script makes searches for alphabetic characters case insensitive. I use ksh, but bash should also work if you want to use it in this script.

Then run the command:

Code:
find . -type f -exec st_gr_pr 'srv6000' {} +

to get a list of all regular files rooted in the current directory that contain srv6000 , SRV6000 , or any of the other six strings with combinations of uppercase and lowercase s, r, and v in that order. Obviously, replace srv6000 if you want to find files containing a different pattern.
works like a charm, perfect answer thanks mate..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Convert binary file to csv and then back to the binary format digidax Shell Programming and Scripting 7 12-27-2012 12:34 PM
split a string and convert to binary srinivasayedla Shell Programming and Scripting 5 09-09-2011 11:05 AM
Insert string in binary file at top param_it UNIX for Advanced & Expert Users 4 05-31-2011 06:15 AM
Split a binary file into 2 basing on 2 delemiter string Averell Shell Programming and Scripting 1 10-30-2008 08:35 AM
replace string in binary file sg1207 Shell Programming and Scripting 2 07-28-2004 11:26 PM



All times are GMT -4. The time now is 08:52 AM.