Search regular expression with search for hex values in files?

Search regular expression with search for hex values in files?

am 06.01.2008 13:19:17 von peter_ha

For a given file aaa.txt I want to check wether it contains a hex value e.g. x'77' (=1 byte)
BUT not a hex sequence x'8877' (=two bytes). In other words byte value x'77' should exist
but it must not NOT be preceded by byte value x'88'.

How can I specify this (pre-)conditions in ONE regular expression and pass it e.g. to grep?

Peter

Re: Search regular expression with search for hex values in files?

am 06.01.2008 13:44:50 von Cyrus Kriticos

Peter Hanke wrote:
> For a given file aaa.txt I want to check wether it contains a hex value e.g. x'77' (=1 byte)
> BUT not a hex sequence x'8877' (=two bytes). In other words byte value x'77' should exist
> but it must not NOT be preceded by byte value x'88'.
>
> How can I specify this (pre-)conditions in ONE regular expression and pass it e.g. to grep?

Strange notation with '.


$ echo "x'77'" | grep "x'[0-7a-f][0-7a-f]'"
x'77'

$ echo "x'7788'" | grep "x'[0-7a-f][0-7a-f]'"


or count matches


$ echo "x'7788'" | grep -c "x'[0-7a-f][0-7a-f]'"
0
$ echo "x'77'" | grep -c "x'[0-7a-f][0-7a-f]'"
1

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Search regular expression with search for hex values in files?

am 06.01.2008 13:46:51 von Cyrus Kriticos

Peter Hanke wrote:
> For a given file aaa.txt I want to check wether it contains a hex value e.g. x'77' (=1 byte)
> BUT not a hex sequence x'8877' (=two bytes). In other words byte value x'77' should exist
> but it must not NOT be preceded by byte value x'88'.
>
> How can I specify this (pre-)conditions in ONE regular expression and pass it e.g. to grep?

Strange notation with '.


$ echo "x'77'" | grep "x'[0-9a-f][0-9a-f]'"
x'77'

$ echo "x'7788'" | grep "x'[0-9a-f][0-9a-f]'"


or count matches


$ echo "x'7788'" | grep -c "x'[0-9a-f][0-9a-f]'"
0
$ echo "x'77'" | grep -c "x'[0-9a-f][0-9a-f]'"
1

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Search regular expression with search for hex values in files?

am 06.01.2008 13:55:21 von Cyrus Kriticos

Cyrus Kriticos wrote:
> Peter Hanke wrote:
>> For a given file aaa.txt I want to check wether it contains a hex
>> value e.g. x'77' (=1 byte)
>> BUT not a hex sequence x'8877' (=two bytes). In other words byte value
>> x'77' should exist but it must not NOT be preceded by byte value x'88'.
>>
>> How can I specify this (pre-)conditions in ONE regular expression and
>> pass it e.g. to grep?
>
[...]

Which preceding values are allowed?

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Search regular expression with search for hex values in files?

am 06.01.2008 16:33:15 von jurgenex

peter_ha@andres.net (Peter Hanke) wrote:
>For a given file aaa.txt I want to check wether it contains a hex value e.g. x'77' (=1 byte)
>BUT not a hex sequence x'8877' (=two bytes). In other words byte value x'77' should exist
>but it must not NOT be preceded by byte value x'88'.
>
>How can I specify this (pre-)conditions in ONE regular expression and pass it e.g. to grep?

Maybe I am missing the point, but shouldn't a simple

[^\x88]\x77

work just fine? Unfortunately the negation of a character class isn't well
explained in 'perldoc perlre'

jue

Re: Search regular expression with search for hex values in files?

am 06.01.2008 18:31:39 von smallpond

On Jan 6, 7:19 am, peter...@andres.net (Peter Hanke) wrote:
> For a given file aaa.txt I want to check wether it contains a hex value e.g. x'77' (=1 byte)
> BUT not a hex sequence x'8877' (=two bytes). In other words byte value x'77' should exist
> but it must not NOT be preceded by byte value x'88'.
>
> How can I specify this (pre-)conditions in ONE regular expression and pass it e.g. to grep?
>
> Peter

This matches \x77 at start of line or preceded by non-\x88.
$string =~ /(^|[^\x88])\x77/;

--S

Re: Search regular expression with search for hex values in files?

am 06.01.2008 21:42:57 von RedGrittyBrick

Peter Hanke wrote ...
> For a given file aaa.txt I want to check wether it contains a hex value
> e.g. x'77' (=1 byte) BUT not a hex sequence x'8877' (=two bytes). In other
> words byte value x'77' should exist but it must not NOT be preceded by
> byte value x'88'.
>
> How can I specify this (pre-)conditions in ONE regular expression

Read `perldoc perlre`
look for *negative lookbehind*:

C> perl -e "@x=qw(aa ab xb b); print qq($_\n) for grep /(? xb
b

Perl likes hex too (documented in the same place):

C> perl -e "@x=qw(aa ab xb b);
print qq($_\n) for grep /(? xb
b


> and pass it e.g. to grep?

Presumably you mean perl's built in grep function rather than the
separate program of the same name.

C> perl -e "@x=qw(aa ab xb b);
$re='(? print qq($_\n) for grep /$re/,@x"
xb
b


I haven't answered the first part of your question, I assume you already
know how to apply grep to a file and count the matches. Ditto
terminating the grep after the first match.

Re: Search regular expression with search for hex values in files?

am 06.01.2008 21:54:19 von rvtol+news

Peter Hanke schreef:

> For a given file aaa.txt I want to check wether it contains a hex
> value e.g. x'77' (=1 byte) BUT not a hex sequence x'8877' (=two
> bytes). In other words byte value x'77' should exist but it must not
> NOT be preceded by byte value x'88'.
>
> How can I specify this (pre-)conditions in ONE regular expression and
> pass it e.g. to grep?

Where you say 'hex value' I assume you just mean 'character value'
(conveniently presented in hexadecimal format).

m/(?
See `perldoc perlre`, look for "look-behind assertion".

--
Affijn, Ruud

"Gewoon is een tijger."

Re: Search regular expression with search for hex values in files?

am 06.01.2008 21:54:19 von rvtol+news

Peter Hanke schreef:

> For a given file aaa.txt I want to check wether it contains a hex
> value e.g. x'77' (=1 byte) BUT not a hex sequence x'8877' (=two
> bytes). In other words byte value x'77' should exist but it must not
> NOT be preceded by byte value x'88'.
>
> How can I specify this (pre-)conditions in ONE regular expression and
> pass it e.g. to grep?

Where you say 'hex value' I assume you just mean 'character value'
(conveniently presented in hexadecimal format).

m/(?
See `perldoc perlre`, look for "look-behind assertion".

--
Affijn, Ruud

"Gewoon is een tijger."

Re: Search regular expression with search for hex values in files?

am 07.01.2008 13:20:23 von Michael Tosch

Peter Hanke wrote:
> For a given file aaa.txt I want to check wether it contains a hex value e.g. x'77' (=1 byte)
> BUT not a hex sequence x'8877' (=two bytes). In other words byte value x'77' should exist
> but it must not NOT be preceded by byte value x'88'.
>
> How can I specify this (pre-)conditions in ONE regular expression and pass it e.g. to grep?
>
> Peter
>

Given that aaa.txt contains

x'66' x'77'
x'88' x'77'
x'8877'

the following will do it:

grep -v "x'88'[^x]*x'77'" aaa.txt | grep "x'77'"

--
Michael Tosch @ hp : com

Re: Search regular expression with search for hex values in files?

am 07.01.2008 19:13:11 von Jim Gibson

In article <4780c745$0$5949$9b4e6d93@newsspool3.arcor-online.net>,
Peter Hanke wrote:

> For a given file aaa.txt I want to check wether it contains a hex value e.g.
> x'77' (=1 byte)
> BUT not a hex sequence x'8877' (=two bytes). In other words byte value x'77'
> should exist
> but it must not NOT be preceded by byte value x'88'.
>
> How can I specify this (pre-)conditions in ONE regular expression and pass it
> e.g. to grep?

You want to match "exactly two consecutive hex digits preceded by
either the beginning of the string or a non-hex-digit and followed by
either a non-hex-digit or the end of the string". In that case

if(
m{
(?: \A | [^[:xdigit:]] )
[[:xdigit:]]{2}
(?: \z | [^[:xdigit:]] )
}x
) {
print "match\n";
}

should do it.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com