Ah, I hadn't noticed that possibility.
But there's a small problem with the
-ABC options in grep. It puts a
-- separator between each contiguous block of text found.
Code:
$ grep -B1 sampler inputfile.txt
interface TenGigE0/0/0/1
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
--
interface TenGigE0/0/0/3
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
And since, for some inexplicable reason, there's no option in grep to turn this off, you have to then pipe the output through another grep instance (or similar), to remove them.
Code:
grep -B1 sampler inputfile.txt | grep -v -e "--"
(Note that the
-e is necessary here, even though there's only one expression, because otherwise the leading hyphen will make grep think it's a command option rather than a pattern.
grep -v -- "--" will also work.)