【发布时间】:2018-01-29 19:21:35
【问题描述】:
到目前为止,我的代码是。我是初学者。
count=0
for f in $ls do
count+= grep -c "classes" $f
done
echo $count
【问题讨论】:
到目前为止,我的代码是。我是初学者。
count=0
for f in $ls do
count+= grep -c "classes" $f
done
echo $count
【问题讨论】:
count=0
for f in *
do
if [ -f "$f" ] #check if that file is regular file
then
count=$(($count+$(grep -c "classes" "$f")))
fi
done
echo $count
【讨论】:
grep: all: No such file or directory grep: it: No such file or directory grep: takes.jpg: No such file or directory ./script8: line 4: 0+: syntax error: operand expected (error token is "+") 0
grep: untitled folder: Is a directory ./script8: line 4: 8+: syntax error: operand expected (error token is "+") 8
我相信 find 将是满足此要求的最佳选择,请您尝试关注并告诉我这是否对您有帮助。
find . -type f -exec grep -i "test" {} + | wc -l
OR
find . -type f | xargs grep -i "test" | wc -l
您也可以提及您的路径来代替上面的 DOT。
【讨论】:
随便用
grep -R <stringToSearch> <dirName>
例如在当前目录和里面的所有文件中搜索“文本”
grep -R "text" .
如果您想获取出现次数,请使用wc -l 作为管道
grep -R "text" . | wc -l
【讨论】:
grep -rnw <Directory_path> -e "Pattern"
例如,如果你想在~/abc/xyz目录的所有文件中找到“helloWorld”,那么运行以下命令-
grep -rnw ~/abc/xyz -e "helloWorld"
要在当前目录中搜索字符串“HelloWorld”,请使用以下命令-
grep -rnw . -e "helloWorld"
【讨论】: