[Toybox] First look at grep -ABC support.

Rob Landley rob at landley.net
Sun Dec 29 16:23:55 PST 2013


Looking at adding -ABC to grep.

What other options does this interact with? You can't combine leading or 
trailing lines with -o, -c, -s, -l or -q. You have to be in "Show me 
lines, show me sweet little lines" mode. (Ok, too much 80's background 
music around here...)

Also, what happens if you specify -C and -A?

   $ grep -A2 -C3 cross README | wc
      17      73     643
   $ grep -C3 cross README | wc
      18      76     660
   $ grep -A2 -B1 -C3 cross README | wc
      15      62     569

Ok, so -A and -B dominate over -C. So what happens if you do:

   $ grep -A2 -o cross README
   cross
   --
   cross
   --
   cross
   cross

What? what are the -- entries... Ah, it's adding -- lines to the output 
to deliniate between context chunks. Does the man page say to...  yes, 
it does:

   -A NUM, --after-context=NUM
     Print NUM  lines  of  trailing  context  after  matching  lines.
     Places   a  line  containing  a  group  separator  (--)  between
     contiguous groups of matches.  With the  -o  or  --only-matching
     option, this has no effect and a warning is given.

Let's count the ways that's wrong, shall we? There was no warning when I 
did -o despite what the man page said. It would have no effect for -c or 
-l either so why single out -o? And it offers no obvious way to suppress 
the -- or change it to something else. Plus the -o version is 
theoretically useful if you want to know how many times these lines 
occur near each other (notice we have a pair above) so that's not "no 
effect".

Hmmm, does this leading/trailing data cross file boundaries?

   $ grep -A 2 two <(echo -e "one\ntwo\nthree") \
       <(echo -e "and one\nand two\nand three")
   /dev/fd/63:two
   /dev/fd/63-three
   --
   /dev/fd/62:and two
   /dev/fd/62-and three

Apparently not. Good to know.

Grumble grumble. Ok, off to implement this mess...

Rob


More information about the Toybox mailing list