I'm looking to pull multiple pieces from a file, or at least watch for pieces in a log file if they should occur, but I don't want to setup multiple tail | grep
sessions. Instead I'd like to just tail the output of each grep.
I suppose I could do this with awk
, but perhaps there is something closer to this idea already.
For example the command might look something like this:
tail -f /var/log/syslog | teegrep cron1 -f CRON | teegrep cloud-init2 -f CLOUD-INIT
The output would be the files CRON and CLOUD-INIT, while all things would go to stdout by the end. Only the matches for 'cron1' and 'cloud-init2' lines would wind up in their respective files.
Maybe it's already part of a command and I have just not known about it. Or possibly just some bash/zsh trickery that would do the same thing.
Either way: Is there a command that combines tee
and grep
such that in a pipeline the tee part can direct matches to a file?
tail -f /var/log/syslog | pee 'grep cron1 > CRON' 'grep cloud-init2 > CLOUD-INIT'
? See 'tee' for commandstail | grep
" That needs some rationale. Ateegrep
as you describe (which could be implemented with process substitutions as in @Kusalananda's answer or with an awk script), whould not use less resources. If anything, it will be slower, because thetail | grep
s will run separately, without having to wait for each other as the "teegrep"s in a single pipeline.