Skip to content

Peculiarity of -B and -A command line options of bgrep #26

@sdbbs

Description

@sdbbs

In normal grep, I'm used to e.g. -B2 to mean "two lines before current match", and -A2 to mean "two lines after current match", for instance:

$  echo -e '1\n2\n3\n4\n5\n6\n7\n8\n' | grep -B2 -A2 '4\|5' --color=always | cat --show-nonprinting
2
3
^[[01;31m^[[K4^[[m^[[K
^[[01;31m^[[K5^[[m^[[K
6
7

By analogy, my first thought is to apply the same interpretation for -B and -A command line options of bgrep as well, except expressed in bytes; however that interpretation does not seem to apply to bgrep - with -A2 and -B2 in the command below, I hoped to obtain two bytes before the match, and two bytes after the match, so 6 bytes in al:

$  echo -ne '\x01\x02\x03\x04\x05\x06\x07\x08' > test.bin && bgrep -B2 -A2 0405 test.bin
test.bin: 00000003
\x02\x03\x04\x05

.... however, I get only 4 matches. It seems, the problem is the -A switch, and in it, we have to add the expected length of the match, and the desired amount of bytes after, to obtain the desired behavior - meaning we need to use -B2 -A4 to obtain "two bytes before the match, and two bytes after the match": in the printout:

$  echo -ne '\x01\x02\x03\x04\x05\x06\x07\x08' > test.bin && bgrep -B2 -A4 0405 test.bin
test.bin: 00000003
\x02\x03\x04\x05\x06\x07
```

It would be nice to have this fixed - or at least to have a note in the help text.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions