Arrays 如何解析stdout的输出

Arrays 如何解析stdout的输出,arrays,node.js,exec,stdout,child-process,Arrays,Node.js,Exec,Stdout,Child Process,例如,如果在nodejs子进程中执行如下操作: exec("find /path/to/directory/ -name '*.txt'", callback); 如何将回调函数中的流式输出解析为数组以获得类似的结果 ['file-1.txt', 'file-2.txt', 'file-3.txt', ...] 电流输出如下所示: path/to/file-1.txt path/to/file-2.txt path/to/file-3.txt 谢谢你的帮助 const exec = req

例如,如果在nodejs子进程中执行如下操作:

exec("find /path/to/directory/ -name '*.txt'", callback);
如何将回调函数中的流式输出解析为数组以获得类似的结果

['file-1.txt', 'file-2.txt', 'file-3.txt', ...]
电流输出如下所示:

path/to/file-1.txt
path/to/file-2.txt
path/to/file-3.txt
谢谢你的帮助

const exec = require('child_process').exec;
exec("find /path/to/directory -name '*.txt'", (error, stdout, stderr) => {
    if (error) {
        // handle error
    } else {
        var fileNames = stdout.split('\n').filter(String).map((path) => {
            return path.substr(path.lastIndexOf("/")+1);
        });
        console.log(fileNames); // [ 'file1.txt', 'file2.txt', 'file3.txt' ]
    }
});


回调在这里返回什么参数??函数回调(错误,标准输出,标准输出){…}返回的输出是这样的:
path/to/file1.txt
path/to/file1.txt
回调的返回输出是这样的:
path/to/file1.txt

path/to/file2.txt

path/to/file3.txt
这可能会对您有所帮助--
const exec = require('child_process').exec;
exec("ls /path/to/directory | grep .txt", (error, stdout, stderr) => {
    if (error) {
        // handle error
    } else {
        var fileNames = stdout.split(/[\r\n|\n|\r]/).filter(String);
        console.log(fileNames); // [ 'file1.txt', 'file2.txt', 'file3.txt' ]
    }
});