|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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." |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|