查找,然后 grep,然后遍历文件列表
find and then grep and then iterate through list of files
我有以下脚本来替换文本。
grep -l -r "originaltext" . |
while read fname
do
sed 's/originaltext/replacementText/g' $fname > tmp.tmp
mv tmp.tmp $fname
done
现在在这个脚本的第一条语句中,我想做这样的事情。
find . -name '*.properties' -exec grep "originaltext" {} \;
我该怎么做?
我在 AIX 上工作,所以 --include-file 不起作用。
您可以反过来,将“*.properties”文件列表提供给 grep。例如
grep -l "originaltext" `find -name '*.properties'`
哦,如果你使用的是最近的 linux 发行版,grep 中有一个选项可以实现这一点,而无需创建那么长的文件列表作为参数
grep -l "originaltext" --include='*.properties' -r .
一般来说,我更喜欢使用 find
来查找文件而不是 grep
。看起来很明显:)
使用 可以将 find
的结果提供给 while
循环:
while IFS= read -r fname
do
sed 's/originaltext/replacementText/g' $fname > tmp.tmp
mv tmp.tmp $fname
done < <(find . -name '*.properties' -exec grep -l "originaltext" {} \;)
注意我使用 grep -l
(大 L
)以便 grep
只是 returns 与模式匹配的文件的名称。
我有以下脚本来替换文本。
grep -l -r "originaltext" . |
while read fname
do
sed 's/originaltext/replacementText/g' $fname > tmp.tmp
mv tmp.tmp $fname
done
现在在这个脚本的第一条语句中,我想做这样的事情。
find . -name '*.properties' -exec grep "originaltext" {} \;
我该怎么做?
我在 AIX 上工作,所以 --include-file 不起作用。
您可以反过来,将“*.properties”文件列表提供给 grep。例如
grep -l "originaltext" `find -name '*.properties'`
哦,如果你使用的是最近的 linux 发行版,grep 中有一个选项可以实现这一点,而无需创建那么长的文件列表作为参数
grep -l "originaltext" --include='*.properties' -r .
一般来说,我更喜欢使用 find
来查找文件而不是 grep
。看起来很明显:)
使用 find
的结果提供给 while
循环:
while IFS= read -r fname
do
sed 's/originaltext/replacementText/g' $fname > tmp.tmp
mv tmp.tmp $fname
done < <(find . -name '*.properties' -exec grep -l "originaltext" {} \;)
注意我使用 grep -l
(大 L
)以便 grep
只是 returns 与模式匹配的文件的名称。