I implemented this in node.js - I don't know of a unix utility that can dynamically change grep. Here is how it works:
before dygrep:
many-child-procs > logfile.log # in terminal 1
tail -f logfile.log | grep <expression> # in terminal 2
after dygrep:
many-child-procs | dygrep # in terminal 1
then you connect to dygrep with a tcp-client and control which expressions are filtered on.
The following node.js process reads from stdin, and uses a tcp connection to listen for expressions to filter on. For each line of stdin, it only forwards that to stdout if it matches one of the current regular expressions stored in memory.
#!/usr/bin/env node
'use strict';
import * as readline from 'readline';
import * as net from 'net';
import chalk from 'chalk';
import {createParser} from "./json-parser";
import log from './logger';
let port = 4900;
const rl = readline.createInterface({
input: process.stdin.resume()
});
const regex = new Map<string, RegExp>();
rl.on('line', l => {
if (regex.size < 1) {
process.stdout.write(l + '\n');
return;
}
for (let [k, v] of regex) {
if (v.test(l)) {
process.stdout.write(chalk.magenta(' (filtered) ') + l + '\n');
break;
}
}
});
interface IncomingTCPMessage {
command: {
add: string,
remove: string,
list: boolean,
removeall: boolean
}
}
const server = net.createServer(s => {
const sendMessage = (m: any) => {
s.write(JSON.stringify({message: m}) + `\n`);
};
s.pipe(createParser()).on('data', (d: IncomingTCPMessage) => {
if (!d.command) {
log.error('No "command" field was found:', d);
return ''
}
const c = d.command;
if (c.list) {
sendMessage({regexes: Array.from(regex.keys()).map(k => ({regex: regex.get(k), str: k}))});
log.info('Listing all regex for the client.');
return;
}
if (c.removeall) {
regex.clear();
sendMessage(`Cleared all regex.`);
log.info('Cleared all regex.');
return;
}
if (c.add) {
regex.set(c.add, new RegExp(c.add));
sendMessage(`Added regex: ${c.add}.`);
log.info('Added regex:', c.add);
return;
}
if (c.remove) {
regex.delete(c.remove);
sendMessage(`Deleted regex: ${c.remove}.`);
log.info('Removed regex:', c.remove);
return;
}
log.error('No matching field was found:', d);
log.info('Regex:', regex);
});
});
server.listen(port);
to see the complete code, see:
https://github.com/ORESoftware/dygrep
bar
without stopping the tail process?