Often, when I am writing a shell script, especially quick one liners, I find myself wishing for a filters similar to grep but, a little different than the standard options provide for.
Basically, what I would want this filter to do is to activate/deactivate on predicates (sometimes regular expression matching like grep, sometimes on particular line numbers) and to pass data or not pass data through the pipe until it is deactivated/activated. I know this sounds weird, but maybe an example can illustrate what I'm talking about.
I a really simple case, say I want to do something with the output of the "route" command. Now nowmally, route shows something like:
Code:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
Notice how there are a couple lines of header info before the actual route lines. If I had a pipeline that processes each record uniformly, the two header lines throw a wrench in the works. What I'd like to do is something like:
Code:
$ route | nfilter --pass 3 | res-of-pipeline
What this would do is only write the third and successive rows to standard output.
Or consider a config file with stanzas like:
Code:
[irrelevant-section-1]
blah
blah
blah
[relevant-section]
interesting
interesting
interesting
[irrelevant-section-2]
blah
blah
blah
If I want to pass the interesting parts to the rest of the pipe:
Code:
$ cat config | filter --pass '\[relevant' '\['
The idea here being that once a line matched the first expression, successive lines would pass until a line matched the second expression.
Anyway, what I want to know if some standard command line tools like that exist, and if they do, what are they called. If not, is it because it is just considered easy enough to do with the existing tools?