Grep on a binary file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Grep on a binary file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-17-2010
canduc17's Avatar
canduc17 canduc17 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 21 September 2010, 5:15 AM EDT
Location: Italy
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Grep on a binary file

Hi everyone.

I have a binary file in wich there is a date with format DDMMMYY, for example 02May09.

I can see it opening this file with vim: inside a binary mess, I can clearly read that string.

Now: do you know a way to extract this string from the file?
I woul like to do something like
Code:
grep "Jan | Feb | ... | Dec" binaryFile

Thanks in advance.
Sponsored Links
    #2  
Old 09-17-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 July 2014, 8:40 AM EDT
Location: Zürich
Posts: 8,421
Thanks: 328
Thanked 1,028 Times in 899 Posts
Hi.

Perhaps with strings ?

Code:
strings binaryFile | grep -E "Jan|Feb|..."

Sponsored Links
    #3  
Old 09-17-2010
canduc17's Avatar
canduc17 canduc17 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 21 September 2010, 5:15 AM EDT
Location: Italy
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Yeah, great!

I'm almost done.

The remaining problem is that this date is on the same line with a lot of other stuff and when I launch your command, the whole line is retrieved.
I obtain only the date with
Code:
strings binaryFile | grep -E "Sep" | awk '{ print $21 }'

because there are only strings, separated by blanks or tabs.

But I have a lot of those file to parse...what if this date is not always at position 21?

Is there a way to generalize it?
    #4  
Old 09-17-2010
john1212 john1212 is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 25 May 2012, 11:08 AM EDT
Posts: 141
Thanks: 0
Thanked 19 Times in 19 Posts
Try:

Code:
sed -n 's/[0-9][0-9]Jan\|Feb\|...Sep\|Oct\|Nov\|Dec[0-9][0-9]/&/g'

Sponsored Links
    #5  
Old 09-17-2010
canduc17's Avatar
canduc17 canduc17 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 21 September 2010, 5:15 AM EDT
Location: Italy
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Excuse me, but I don't understand how to use your command.
I've tryed
Code:
strings binaryFile | sed -n 's/[0-9][0-9]Jan\|Feb\|Mar\|Apr\|Jun\|Jul\|Aug\|Sep\|Oct\|Nov\|Dec\[0-9][0-9]/&/g'

and several other combination with the commands above, but it didn't works...
Could you please post the complete command?
Sponsored Links
    #6  
Old 09-17-2010
john1212 john1212 is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 25 May 2012, 11:08 AM EDT
Posts: 141
Thanks: 0
Thanked 19 Times in 19 Posts

Code:
cat binaryFile | sed -n 's/[0-9][0-9]Jan\|Feb\|...Sep\|Oct\|Nov\|Dec[0-9][0-9]/&/g'

It can don't work if line is too long, can be error. lenght from newline to newline.
some distribution sed no do:

Code:
\|

you can do:

Code:
cat binaryFile | sed -n 's/[0-9][0-9]Jan[0-9][0-9]/&/g';'s/[0-9][0-9]Feb[0-9][0-9]/&/g';'s/[0-9][0-9]Mar[0-9][0-9]/&/g'

your try is good too:

Code:
strings binaryFile | sed -n 's/[0-9][0-9]Jan[0-9][0-9]/&/g';'s/[0-9][0-9]Feb[0-9][0-9]/&/g';'s/[0-9][0-9]Mar[0-9][0-9]/&/g'


Last edited by john1212; 09-17-2010 at 08:20 AM..
Sponsored Links
    #7  
Old 09-17-2010
canduc17's Avatar
canduc17 canduc17 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 21 September 2010, 5:15 AM EDT
Location: Italy
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts

Code:
user@localhost ~ $ cat binaryFile | sed -n 's/[0-9][0-9]Jan\|Feb\|Mar\|Apr\|Jun\|Jul\|Aug\|Sep\|Oct\|Nov\|Dec\[0-9][0-9]/&/g'
user@localhost ~ $ strings binaryFile | sed -n 's/[0-9][0-9]Jan\|Feb\|Mar\|Apr\|Jun\|Jul\|Aug\|Sep\|Oct\|Nov\|Dec\[0-9][0-9]/&/g'
user@localhost ~ $ strings binaryFile | sed -n 's/[0-9][0-9]Sep[0-9][0-9]/&/g'
user@localhost ~ $

No errors reported, but it doesn't work...
If possible, the simpler way is to use this (which works):
Code:
user@localhost ~ $ strings binaryFile | grep -E "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec" | awk '{ print $21 }'
09Sep09
candini@MEEO-office-007 ~ $

but instead to use "print $21", use a parametric "print $(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)".

But I don't know how to implement something like that...
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
Binary file joshighanshyam Programming 2 05-22-2009 12:23 PM
dd - binary file spiriad Shell Programming and Scripting 3 12-05-2008 07:35 AM
Binary txt file received when i use uuencode to send txt file as attachment ash22 UNIX for Dummies Questions & Answers 2 04-24-2008 08:03 PM
compiled binary file gives "cannot execute binary file" scgupta Solaris 0 07-13-2006 01:59 AM
binary file markms UNIX for Dummies Questions & Answers 1 04-07-2006 09:40 AM



All times are GMT -4. The time now is 12:06 PM.