我想搜索数百个xml文件中是否存在一些关键词,我想使用以下脚本来处理这个问题:
#!/usr/local/bin/bash
find . -name '*.xml' |xargs egrep -n "HERE IS LONG LIST(word1|word2|...)" > result
我收到错误消息:
xargs: a single arg was greater than the max arglist size of 2048 characters
所以我将长列表改为3部分,它变成了:
#!/usr/local/bin/bash
find . -name '*.xml' |xargs egrep -n "LIST_1" > result
find . -name '*.xml' |xargs egrep -n "LIST_2" >> result
find . -name '*.xml' |xargs egrep -n "LIST_3" >> result
有哪些更好的方法来处理这个以避免模式列表分离的事情?
答案 0 :(得分:6)
更好的方法是将所有匹配模式存储在一个文件中,并使用-f
开关的递归grep:
grep -n -f patternFile -R --include=*.xml .
答案 1 :(得分:1)
grep --help:
f, - file = FILE从FILE
获取PATTERN
所以你可以:
echo "HERE IS LONG LIST(word1|word2|...)" > pattern.txt
find . -name '*.xml' |xargs egrep -n -f pattern.txt > result