find string using grep

Go To StackoverFlow.com

1

Could anyone show me how I can find all strings in a file which are alphanumeric and may contain either the symbol _ or # and end in the hex code 0x00. I've tried using grep with the following options but it doesn't seem to work: -z [a-zA-Z0-9_]*

Thanks in advance.

Update:

Here's an example of some of the strings im trying to extract, as you can see they end with the hex code 0x00, vary in length and although this specific example doesn't show they can contain 0-9, an underscore (_) or a hash (#).

http://i42.tinypic.com/23kos5w.jpg

2012-04-03 23:20
by Twisted89
did you try using 'grep -E'? Also, you don't have the # in your regexp. And did you try using 'sed' or, if you're checking a binary file, 'strings' - drewbenn 2012-04-03 23:24
Can you edit your question and show us some samples of what you want to match? Do you want the strings to end with an actual NUL character, or the string "0x00"? AFAIK, grep doesn't have notation to search for NULs. The regex [a-zA-Z0-9_]+ matches 1 or more of the characters as you'd expect, but the + (not a *) will require the -E option to grep. And always put your regex in quotes - ghoti 2012-04-03 23:38
Command: grep -z -E "[\w#]*" Should do the trick - MrJames 2012-04-03 23:30
@ghoti you're wrong \w stands for "word character", usually [A-Za-z0-9_ - MrJames 2012-04-03 23:39
More? I meant less. Per grep's man page, \w is a synonym for [:alnum:], and [[:alnum:]] means [0-9A-Za-z]. And why are you escaping the #? And how are you handling the null? Note that the -z or --null-data option to GNU grep doesn't exist in version 2.5.1, which is what's used in FreeBSD and Mac OS X. Also doesn't exist in non-GNU grep. The OP didn't specify Linux - ghoti 2012-04-03 23:48
The grep command should be 'grep -E' to use POSIX character classes. And you're wrong, the -z command exists on GNU grep >= 2.6.3 - MrJames 2012-04-04 00:02
Ah, right you are - the option exists in the binary, it merely seems to have been dropped from the grep man page in both FreeBSD and OS X. Still doesn't exist in Solaris or HP/UX. The OP didn't specify Linux - ghoti 2012-04-04 00:06
When I run this all I get is 'Binary file /cygdrive/d/dump.bin matches'? Im using grep in cygwin - Twisted89 2012-04-04 10:09


0

When I run this all I get is 'Binary file /cygdrive/d/dump.bin matches'? Im using grep in cygwin. – Twisted89 Apr 4 at 10:09

MrJames answer does not include the -a. Plus his putting \w in brackets simply doesnt work.

grep -Eaoz "(\w|_|#)*" FILE
2012-07-24 15:24
by Steven Penny
Ads