简体   繁体   中英

Executing zgrep with node.js

I need to execute the zgrep command through a node.js app. I have managed to add the grep command to my app like so:

const grep = require('grep1');

grep(['greptext', './logs/file], function(err, stdout, stderr) {
        if (err || stderr) {
            console.log(err, stderr);
        } else {
            console.log(stdout);
        }
    });

This works fine for text files but I need to do the same for gz files using the zgrep command. Is there an npm package that can do this for me?

Managed to do this using the child_process package. Sample implementation below:

exec = require('child_process').exec;

child = exec('zgrep \'search text\' ./logs/file',
        function (error, stdout, stderr) {
            console.log('stdout: ' + stdout);
            console.log('stderr: ' + stderr);
            if (error !== null) {
                console.log('exec error: ' + error);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM