Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep print byte-offset without printing match

I am using the grep command on a binary file (-a flag included) and want the output of the --binary-offset or -b flag without actually printing the match, since the binary data is useless to me and causes the terminal to behave strangely (my guess is that the terminal is interpreting some of the binary as terminal commands).

Currently, I'm using the command:

dd ... 2>/dev/null | grep -abFf - file_to_search

to grep for a binary string (result of dd) in file_to_search

The output looks like:

offset:[large string of binary characters]

How can I print just the offsets (without the large string of binary characters)?

like image 353
arcyqwerty Avatar asked Nov 01 '22 13:11

arcyqwerty


1 Answers

It is not very clear what is inside of the "large string of binary characters" but ading an extra "grep -o Number in the beginning of the line" might do what you want:

 dd ... 2>/dev/null | grep -abFf - file_to_search | grep -oP  '^\d+:'

If you want just the first offset:

 ...| grep -oP  -m 1 '^\d+:'
like image 135
JJoao Avatar answered Nov 13 '22 14:11

JJoao