Grep on a binary file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep on a binary file
# 8  
Old 09-17-2010
Code:
# cat infile
02May09 test data echo "this is a test" \^$$%$#%# May
test data echo "this is a test" \^$$%$#% 02Jul10 #
test data echo 02Aug11 "this is a test" \^$$%$

Code:
for i in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
do
strings infile | sed -n "s/.*\([0-9][0-9]$i[0-9][0-9]\).*/\1/p"; done
02May09
02Jul10
02Aug11

# 9  
Old 09-17-2010
Code:
user@localhost ~ $ for I in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ; do strings binaryFile | sed -n "s/.*\([0-9][0-9]${I}[0-9][0-9]\).*/\1/p" ; done
09Sep09
user@localhost ~ $

It works, but it's really slow...5 seconds to obtain the results.
I know, Bash is not C, but is there something faster?
No one knows the solution using awk as I suggested in my previous post?
Code:
strings binaryFile | grep -E "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec" | awk '{ print $(variable with month inside) }'

# 10  
Old 09-17-2010
Of course:
CODE]cat binaryFile | sed -n 's/[0-9][0-9]Jan\|Feb\|Mar[0-9][0-9]/&/gp'[/CODE]

Last edited by john1212; 09-17-2010 at 09:45 AM..
# 11  
Old 09-17-2010
What gives the following?
Code:
strings binaryFile | grep -Eo "[0-9]+Sep[0-9]+"

# 12  
Old 09-17-2010
Code:
strings infile | grep -Eo '[0-9][0-9]May[0-9][0-9]|[0-9][0-9]Jul[0-9][0-9]|[0-9][0-9]Aug[0-9][0-9]'
02May09
02Jul10
02Aug11

# 13  
Old 09-17-2010
@john1212
Code:
user@localhost ~ $ cat binaryFile | sed -n 's/[0-9][0-9]Jan\|Feb\|Mar[0-9][0-9]/&/g/p'
sed: expression -e #1, char 40: `s' option unknown
user@localhost ~ $ strings binaryFile | sed -n 's/[0-9][0-9]Jan\|Feb\|Mar[0-9][0-9]/&/g/p'
sed: expression -e #1, char 40: `s' option unknown
user@localhost ~ $

@Frans
Code:
user@localhost ~ $ strings binaryFile | grep -Eo "[0-9]+Sep[0-9]+"
09Sep09
user@localhost ~ $

Great, it works!
How could I add the other 11 months?

---------- Post updated at 07:59 AM ---------- Previous update was at 07:53 AM ----------

Thanks ygemici!
This works perfectly:
Code:
user@localhost ~ $ strings binaryFile | grep -Eo '[0-9][0-9]Jan[0-9][0-9]|[0-9][0-9]Feb[0-9][0-9]|[0-9][0-9]Mar[0-9][0-9]|[0-9][0-9]Apr[0-9][0-9]|[0-9][0-9]May[0-9][0-9]|[0-9][0-9]Jun[0-9][0-9]|[0-9][0-9]Jul[0-9][0-9]|[0-9][0-9]Aug[0-9][0-9]|[0-9][0-9]Sep[0-9][0-9]|[0-9][0-9]Oct[0-9][0-9]|[0-9][0-9]Nov[0-9][0-9]|[0-9][0-9]Dec[0-9][0-9]'
09Sep09
user@localhost ~ $

And it is also very fast.

Frans, if you're still there, I'm interested in your solution also!
# 14  
Old 09-17-2010
You can combine both (ygeminici's and mine)
Code:
strings binaryFile | grep -Eo '[0-9]+Jan[0-9]+|[0-9]+Feb[0-9]+|[0-9]+Mar[0-9]+|[0-9]+Apr[0-9]+|[0-9]+May[0-9]+|[0-9]+Jun[0-9]+|[0-9]+Jul[0-9]+|[0-9]+Aug[0-9]+|[0-9]+Sep[0-9]+|[0-9]+Oct[0-9]+|[0-9]+Nov[0-9]+|[0-9]+Dec[0-9]+'

or try this one:
Code:
strings binaryFile | grep -Eo '[0-9][0-9][A-Z][a-z][a-z][0-9][0-9]'

This User Gave Thanks to frans For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

2. UNIX for Dummies Questions & Answers

[AIX] Binary file warning for text file.

Hello guys, We had to move from a DC to another, and we are now facing an "issue" with some text files. Looks like that some of our log files are set as binary: file TuxConnectorURA.20121012 TuxConnectorURA.20121012: data or International Language text less TuxConnectorURA.20121012... (2 Replies)
Discussion started by: EnioMarques
2 Replies

3. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

4. UNIX for Advanced & Expert Users

How to copy a binary file while the file is being written to by another process

Hello, Can I copy a binary file while the file is being written to by another process? Another process (program) “P1” creates and opens (for writing) binary file “ABC” on local disk. Process P1 continuously write into ABC file every couple of seconds, adding 512-byte blocks of data. ABC file... (1 Reply)
Discussion started by: mbuki
1 Replies

5. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

6. Shell Programming and Scripting

To log binary file output to a txt file

Hi, I wrote a small script whose function is to execute the postemsg provided if the threshold breaches. I want to log this postemsg messages to a log file. But I am not able to do. Can someone throw some light on how to log the output of this. I am pasting a snippet of that code. ... (2 Replies)
Discussion started by: dbashyam
2 Replies

7. Programming

Binary file

Hi all I have a C program to write it's output into a textfile which is also created by the program in between. The file that's get created prompt's a message while opening manually that the file is binary and any change in that leads to it's corruption. I have used putc() to write the output... (2 Replies)
Discussion started by: joshighanshyam
2 Replies

8. Shell Programming and Scripting

dd - binary file

I have a task that says: make a file (called binaryfile ) that contains 4 bytes of NULL data 6 bytes of random data 8 bytes of 1 10 bytes of 5 and 12 bytes of 9. For the first 2, I can used : dd if=/dev/null of=binaryfile bs=8 count =4 and dd if=/dev/urandom of=binaryfile bs=8... (3 Replies)
Discussion started by: spiriad
3 Replies

9. Solaris

compiled binary file gives "cannot execute binary file"

Hi, I have two Solaris machines. 1. SunOS X 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Blade-1500 2. SunOS Y 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-60 I am trying to buiild a project on both these machines. The Binary output file compiled on machine 2 runs on both the machines. Where... (0 Replies)
Discussion started by: scgupta
0 Replies

10. UNIX for Dummies Questions & Answers

binary file

please let me know how can i mail the binary files is it can be done thru pine? is there any other way to do it? wat are the changes in system i have to make and one more thing i am sending data to a message queue and then retriving the data from the queue but when i do the ipcs... (1 Reply)
Discussion started by: ramneek
1 Replies
Login or Register to Ask a Question