我应该找到并显示包含单词胡萝卜的文件数(忽略大小写)
到目前为止,这就是我所拥有的,我只是不确定如何将 wc 添加到其中以计算有多少文件带有胡萝卜一词
找 。-exec grep -i胡萝卜{} \;
ter*_*don 13
首先,正如其他人所说,没有理由使用find,只需使用递归grep:
grep -irm 1 carrot . | wc -l
Run Code Online (Sandbox Code Playgroud)
该-m 1保证grep将停止后的首场比赛搜索每个文件。没有它,您不计算包含的文件数,carrot而是计算行数,如果同一个文件包含多个carrot. 来自man grep:
-r, --recursive
Read all files under each directory, recursively, following
symbolic links only if they are on the command line. This is
equivalent to the -d recurse option.
-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input
files. (-i is specified by POSIX.)
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.
Run Code Online (Sandbox Code Playgroud)
如果你真的,真的想用 find 来做,你可以做
find . -type f -exec grep -im 1 carrot {} \; | wc -l
Run Code Online (Sandbox Code Playgroud)
请注意,我正在指定,-type f因为您不想grep目录。
查找包含单词“carrot”的文件数
number_of_files=`grep -l -r -i "carrot" . | wc -l`
Run Code Online (Sandbox Code Playgroud)
参数的含义grep:
-l, --files-with-matches
Only the names of files containing selected lines are written to standard output. grep will only search a file until a match has been found, making
searches potentially less expensive. Pathnames are listed once per file searched. If the standard input is searched, the string ``(standard
input)'' is written.
-R, -r, --recursive
Recursively search subdirectories listed.
-i : case insenstive
Run Code Online (Sandbox Code Playgroud)
wc -l:打印出作为程序输入传递的行数。在我们的例子中,这些行是具有由 找到的匹配输入模式的文件的名称grep。
打印输出
echo $number_of_files
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21972 次 |
| 最近记录: |