简体   繁体   English

Bash Mapfile 或 Read while grep-ing 文件内容

[英]Bash Mapfile or Read while grep-ing contents of a file

Just looking for any other, perhaps better ways to do the following.只是寻找其他任何可能更好的方法来执行以下操作。 Shell check is calling "SC2002 Useless cat" and every other way I have tried to mitigate has failed. Shell 检查正在调用“SC2002 Useless cat”,而我尝试缓解的所有其他方法都失败了。 It is working as desired, so I am inclined to just leave it alone - unless there are any other ideas out there.它按预期工作,所以我倾向于不理会它 - 除非那里有任何其他想法。

#!/bin/bash
y=0
mapfile -O "$y" -t "ARRAY_NAME" < <(cat "${FILENAME}" | grep "pattern 1" | grep "pattern 2")

Works fine, but hits SC2002 for useless cat.工作正常,但为了无用的猫而命中 SC2002。 So, I've tried to obvious:所以,我试图显而易见:

mapfile -O "$y" -t "ARRAY_NAME" < <(grep -e "pattern 1" -e "pattern 2" "${FILENAME}")

No joy - results in "command grep not found"没有乐趣 - 导致“找不到命令 grep”

Now, I can simply read the file, line-by-line into an array with mapfile, but I can't grep out only the lines I need - I can also do the same with read, which is probably better, but have the same grep problems.现在,我可以简单地将文件逐行读取到带有 mapfile 的数组中,但我不能只提取出我需要的行 - 我也可以对 read 做同样的事情,这可能更好,但是有同样的grep问题。

So any ideas?那么有什么想法吗? Can I just ignore SC2002 in this case?在这种情况下,我可以忽略 SC2002 吗?

Using 'cat filename' (single file) at the beginning of a pipeline can usually be eliminated by redirecting the standard input of the NEXT command in the pipe into the file.通过将管道中 NEXT 命令的标准输入重定向到文件中,通常可以消除在管道开头使用“cat filename”(单个文件)。

mapfile -O "$y" -t "ARRAY_NAME" < <(grep "pattern 1" <"${FILENAME}"| grep "pattern 2")

Unless you care about every percentage of efficiency, and this line is the bottleneck for performance, safe to ignore, if you want, this message.除非你关心效率的每一个百分比,而且这条线是性能的瓶颈,如果你愿意,可以安全地忽略这条消息。

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

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