
carrot라는 단어가 포함된 파일 수를 찾아 표시해야 합니다(대소문자 무시).
지금까지 이것이 내가 가지고 있는 것입니다. carrot이라는 단어가 포함된 파일이 몇 개 있는지 계산하기 위해 wc를 여기에 추가하는 방법을 잘 모르겠습니다.
찾다 . -exec grep -i 당근 {} \;
답변1
우선, 다른 사람들이 말했듯이 사용할 이유가 없습니다. find
그냥 recursive를 사용하세요 grep
.
grep -irm 1 carrot . | wc -l
첫 번째 일치 후 각 파일 검색을 중지 -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.
정말로 find로 하고 싶다면 할 수 있습니다.
find . -type f -exec grep -im 1 carrot {} \; | wc -l
-type f
디렉토리를 원하지 않기 때문에 지정하고 있습니다 grep
.
답변2
해당 단어가 포함된 파일 수 찾기당근
number_of_files=`grep -l -r -i "carrot" . | wc -l`
인수 의 의미 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
wc -l
: 프로그램에 입력으로 전달된 줄 수를 인쇄합니다. 우리의 경우 이 줄은 에서 찾은 입력 패턴과 일치하는 파일의 이름입니다 grep
.
출력물 인쇄
echo $number_of_files
답변3
smRaj 솔루션의 변형은 grep을 두 번 호출하는 것입니다. 다음은 다음과 같은 결과를 제공합니다.grep[등]| 화장실 -l:
grep -l -r -i "carrot" . | grep -c .
다음은 검색된 단어가 포함된 파일의 번호가 매겨진 목록을 인쇄합니다.
grep -l -r -i "carrot" . | grep -n .