Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Grep on a binary file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-17-2010
canduc17's Unix or Linux Image
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 Unix and Linux 09-17-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 26 May 2015, 7:40 PM EDT
Location: Zürich
Posts: 8,553
Thanks: 337
Thanked 1,077 Times in 936 Posts
Hi.

Perhaps with strings ?

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

Sponsored Links
    #3  
Old Unix and Linux 09-17-2010
canduc17's Unix or Linux Image
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 Unix and Linux 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 Unix and Linux 09-17-2010
canduc17's Unix or Linux Image
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 Unix and Linux 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 Unix and Linux 09-17-2010
canduc17's Unix or Linux Image
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:46 PM.