我有一个数组(superStringIgnoreArray)包含超级字符串,如“formula”,“forest”,“foreign”,“fortify”,我正在运行以下grep行:
eval 'find "$SEARCH_DIR" -type f -print0 | xargs -0 grep -HniI "$hitWord" >> "$OUTPUT_FILE"'
eval 'find "$SEARCH_DIR" -type f -print0 | xargs -0 grep -HniI --color=always "$hitWord" | more'
此实例中的hitWord为“for”。
如何返回与superStringIgnoreArray中的任何条目都不匹配的所有匹配? (因此将返回包含“for”,“form”,“fort”“fork”“forming”的行,但“fortify”,“forest”等不会返回。
示例输出:
srcToSearch/open_source_licenses.txt:12:source software packages. One or more such open_source_licenses.txt files may there**for**e
srcToSearch/open_source_licenses.txt:19:-- **For** vCenter Server 5.5u2 GA, the license in**for**mation listed in Parts 2,
srcToSearch/open_source_licenses.txt:22:-- **For** vCenter Server on Linux Virtual Appliance 5.5u2 GA, the license
srcToSearch/open_source_licenses.txt:23:in**for**mation listed in Parts 1, 2, 3, 4, 5 and 6 are applicable.
srcToSearch/open_source_licenses.txt:29:document. This list is provided **for** your convenience; please read further if
答案 0 :(得分:3)
grep
+ bash
解决方案:
superStringIgnoreArray=("formula" "forest" "foreign" "fortify")
grep -HniIr "$hitWord" "$SEARCH_DIR"/* \
| grep -v -f <(printf '%s\n' "${superStringIgnoreArray[@]}") | tee "$OUTPUT_FILE"
答案 1 :(得分:1)
因为你输出的是文件名,所以链接另一个grep并不是一件容易的事,但你可以用awk
$ grep -HniIFr "$hitWord" "$SEARCH_DIR" |
awk 'BEGIN {OFS=FS=":"}
NR==FNR {a[tolower($0)]; next}
{f=$1;n=$2;$1=$2="";
for(k in a) if(tolower($0)~k) next}
{$1=f;$2=n;print}' blacklist -
此处awk
使用:
分隔符限制文件名后的匹配。如果您“hitWord”是文字添加-F
将有所帮助。 awk
仍在进行模式匹配。 tolower()
也是第二步不区分大小写。
由于分隔符“:”可以出现在正文中,我们不能依赖awk
中的$ 3,而是存储$ 1和$ 2;将它们从线上移除,匹配并在打印前将它们添加回来。我想在这一点上你也可以为这个awk添加第一个grep功能。
但是,我认为如果没有-o
标志,当同一行上存在实际匹配和不需要的匹配时,此和其他基于行的解决方案将失败。如果不需要的超弦很少,那么负面的回顾/超前模式可能是更好的解决方案。
如果blacklist
不是文件而是数组,则可以像在其他答案中一样进行文件替换,替换为
... | awk '...' <(printf '%s\n' "${superStringIgnoreArray[@]}") -