PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Search regular expression with search for hex values in files?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Search regular expression with search for hex values in files?

Réponse
 
LinkBack Outils de la discussion
Vieux 06/01/2008, 12h19   #1
Peter Hanke
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Search regular expression with search for hex values in files?

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

  Réponse avec citation
Vieux 06/01/2008, 12h44   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

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.
  Réponse avec citation
Vieux 06/01/2008, 12h46   #3
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

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.
  Réponse avec citation
Vieux 06/01/2008, 12h55   #4
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

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.
  Réponse avec citation
Vieux 06/01/2008, 15h33   #5
Jürgen Exner
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

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
  Réponse avec citation
Vieux 06/01/2008, 17h31   #6
smallpond
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

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
  Réponse avec citation
Vieux 06/01/2008, 20h42   #7
RedGrittyBrick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

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 /(?<!a)b/,@x"
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 /(?<!\x61)\x62/,@x"
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='(?<!a)b';
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.

  Réponse avec citation
Vieux 06/01/2008, 20h54   #8
Dr.Ruud
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

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/(?<!\x88)\x77/

See `perldoc perlre`, look for "look-behind assertion".

--
Affijn, Ruud

"Gewoon is een tijger."

  Réponse avec citation
Vieux 07/01/2008, 12h20   #9
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

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
  Réponse avec citation
Vieux 07/01/2008, 18h13   #10
Jim Gibson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search regular expression with search for hex values in files?

In article <4780c745$0$5949$9b4e6d93@newsspool3.arcor-online.net>,
Peter Hanke <peter_ha@andres.net> 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h49.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17109 seconds with 18 queries