Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-29-2009, 10:09 AM
|
#1
|
|
Member
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704
Rep:
|
scan binary file for word and print words before and after
Hi,
i'd like to scan a binary file for a given word and in case this word is found, print a number of characters (no distinction made between human readable or not) before and a number of character after. For example 10chars+ MY_WORD +10chars.
I am not quite experienced; should i use awk or grep? Use a pattern like a "..........MY_WORD.........."?
Thanks a lot
|
|
|
|
07-29-2009, 05:51 PM
|
#2
|
|
Moderator
Registered: May 2001
Posts: 24,790
|
If SEARCHTERM occupies ${#SEARCHTERM} chars, and if STRING=$(grep /path/to/target -m1 -A1 -B1 -a SEARCHTERM), then CHARSBEFORE=${//SEARCHTERM*/} (and CHARSAFTER=${//*SEARCHTERM/} ), so the offset of CHARSBEFORE would be $[${#CHARSBEFORE}-10], so I think you want to display ${CHARSBEFORE:$[${#CHARSBEFORE}-10]:${#CHARSBEFORE}}. Does that make sense?
|
|
|
|
07-30-2009, 03:59 AM
|
#3
|
|
Member
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704
Original Poster
Rep:
|
Quote:
Originally Posted by unSpawn
If SEARCHTERM occupies ${#SEARCHTERM} chars, and if STRING=$(grep /path/to/target -m1 -A1 -B1 -a SEARCHTERM), then CHARSBEFORE=${//SEARCHTERM*/} (and CHARSAFTER=${//*SEARCHTERM/} ), so the offset of CHARSBEFORE would be $[${#CHARSBEFORE}-10], so I think you want to display ${CHARSBEFORE:$[${#CHARSBEFORE}-10]:${#CHARSBEFORE}}. Does that make sense?
|
Sorry, have rarely used something like this and i do not get it. It could be more help, if you could explain a little. Thanks a lot.
|
|
|
|
07-30-2009, 05:46 AM
|
#4
|
|
Moderator
Registered: May 2001
Posts: 24,790
|
If SEARCHTERM=GNU, then total amount of chars in string is 'echo ${#SEARCHTERM}', and displaying first 2 chars (starts at zero) of it is 'echo ${SEARCHTERM:0:2}'
Take as STRING=$(grep /bin/ls -m1 -A1 -B1 -a GNU), you'll have chars in before and after your SEARCHTERM.
Before is CHARSBEFORE=${//SEARCHTERM*/}.
After is CHARSAFTER=${//*SEARCHTERM/}.
Since you want to display 10 chars starting from the end of the string you need the range to be (total amount of chars minus ten) - (total amount of chars).
When debugging 'set' (as in xve) and 'eval' are your friends:
Code:
eval echo ${#CHARSBEFORE}
eval echo $[${#CHARSBEFORE}-10]
eval echo ${CHARSBEFORE:$[${#CHARSBEFORE}-10]:${#CHARSBEFORE}}
Better?
|
|
|
|
07-30-2009, 10:13 AM
|
#5
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,259
|
unSpawn seems to be trying to show how grep can be used to display leading and trailing context. While I think his commandline example is broken (should be more like 'grep -m1 -A1 -B1 -a GNU /bin/ls'), it seems that he is grabbing one line of 'text' before and after each matching instance, and then, using some bash text manipulation operators, truncating the leading and trailing context to the desired amount of characters. This probably works, but assumes that the leading and trailing context characters are all on the single preceding and succeeding lines (whatever we mean by 'line' in a binary file).
This points to what I see as a fundamental problem with the whole approach: grep, awk, etc., are text oriented tools, which expect newline delimited text, and binary files simply aren't like that. Moreover, displaying raw binary data rarely makes sense, as the effect will be different depending on the terminal in use, and it's runtime configuration. I would suggest re-evaluating your requirements, and/or considering the use of a tool such as od, possibly in combination with grep or other text processor.
--- rod.
|
|
|
|
07-30-2009, 10:30 AM
|
#6
|
|
Moderator
Registered: May 2001
Posts: 24,790
|
I did take his "print a number of characters (no distinction made between human readable or not) before and a number of character after" quite literally. Good advice.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:51 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|