Unix/Linux

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Search regular expression with search for hex values in files?

    5 answers - 410 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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 NT be preceded by byte value x'88'.
    How can I specify this (pre-)conditions in NE regular expression and pass it e.g. to grep?
    Peter
  • No.1 | | 780 bytes | |

    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 NT be preceded by byte value x'88'.

    How can I specify this (pre-)conditions in NE 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
  • No.2 | | 573 bytes | |

    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 NT be preceded by byte value x'88'.
    >>

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

    []

    Which preceding values are allowed?
  • No.3 | | 780 bytes | |

    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 NT be preceded by byte value x'88'.

    How can I specify this (pre-)conditions in NE 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
  • No.4 | | 1163 bytes | |

    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 NT be preceded by
    byte value x'88'.

    How can I specify this (pre-)conditions in NE regular expression

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

    Cperl -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):

    Cperl -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.

    Cperl -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.

  • No.5 | | 662 bytes | |

    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 NT be preceded by byte value x'88'.

    How can I specify this (pre-)conditions in NE 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'"

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


max 4000 letters.
Your nickname that display:
In order to stop the spam: 5 + 4 =
QUESTION ON "Unix/Linux"

EMSDN.COM