简体   繁体   English

使用node.js执行zgrep

[英]Executing zgrep with node.js

I need to execute the zgrep command through a node.js app. 我需要通过node.js应用程序执行zgrep命令。 I have managed to add the grep command to my app like so: 我已经设法将grep命令添加到我的应用中,如下所示:

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. 这对于文本文件很好用,但是我需要使用zgrep命令对gz文件执行相同的操作。 Is there an npm package that can do this for me? 是否有一个npm软件包可以为我做到这一点?

Managed to do this using the child_process package. 可以使用child_process软件包来做到这一点。 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);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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