Node.js grep-f的节点传入字符串——固定字符串

Node.js grep-f的节点传入字符串——固定字符串,node.js,bash,unix,grep,child-process,Node.js,Bash,Unix,Grep,Child Process,我想使用Node.js环境中的grep--count--fixed strings niels.txt

我想使用Node.js环境中的
grep--count--fixed strings niels.txt

我没有为
niels.txt
创建文件,而是有一个字符串数组要搜索,而不是
haystack.txt
我有一个大字符串/文本缓冲区

使用
child\u process
方法的最佳组合是什么

比如:

const needles = ["find", "me", "or", "me"];
const grep_pattern_args = needles.reduce((res, pattern) => {
    res.push('-e', pattern)
    return res
}, [])
const grep_args = [ '--count', '--fixed-strings', ...grep_pattern_args ]
从“child_进程”导入{spawn};
//在草堆中搜索
const haystack=“我是一根如此大的绳子,你看到我了吗?”;
常量可读=新可读();
可读。推(干草堆);
可读。推送(null);
//通常以“--file=pinees.txt”格式显示的针的列表`
常量针=[“查找”、“我”、“或”、“我”];
//产卵'fgrep`
//问:我如何将“针”作为一根线传入?
const fgrep=spawn(`fgrep`,[Piners])
//用烟斗把我的草堆吹到厨房里
可读管道(fgrep.stdin);

对于
grep
args,
-e
允许您指定多个模式:

grep -e 1 -e 2
用于生成参数的JS类似于:

const needles = ["find", "me", "or", "me"];
const grep_pattern_args = needles.reduce((res, pattern) => {
    res.push('-e', pattern)
    return res
}, [])
const grep_args = [ '--count', '--fixed-strings', ...grep_pattern_args ]
3000针正进入打击区域。如果你有很长的针,为了安全起见,你可能需要将它们写入一个文件中

haystack
被读取/生成时,您可以将
stdin
的可写流返回给它(假设
Readable
流示例设置是精心设计的)


使用
grep
命令进行搜索是否是一项硬性要求?可以在js@Matt谢谢-是的,但为了安全起见,作为背景,有3000根针,还有非常大的草堆,这已经被测试为比其他JS方法更快的方法。我对其他方法持开放态度,但仍然需要找出这一实现方式谢谢!特别是针对
MAX_ARG\u STRLEN
使用文件的正当性提示