2009-05-19 5 views
11

카운트 파일 내부 문자열을 하위 디렉토리 많이 가진 디렉토리를 검색 할 필요 : 내가 사용그렙 재귀 및

:

grep -c -r "string here" * 

어떻게 발견의 수를 총 수 있습니까?

하나 이상의 인스턴스가있는 파일 만 출력하려면 어떻게해야합니까?

+0

정확하게 작동하지 않는 것과 같은 세부 정보를 제공 할 수 있습니까? grep의 전체 경로는 무엇이며 어떤 systme를 실행하고 있습니까? – ennuikiller

+0

당신은 당신이보고있는 출력과 당신이 얻고 자하는 것이 당신이 얻는 것과 어떻게 다른지 명확히 할 수 있습니까? – Suppressingfire

답변

9

나를 위해 작동합니다 (각 파일에있는 '여기 문자열'의 총 수를 얻음). 그러나 검색된 모든 파일의 총계는 표시되지 않습니다. 당신이 그것을 얻을 수있는 방법은 다음과 같습니다 :

grep -c -r 'string' file > out && \ 
    awk -F : '{total += $2} END { print "Total:", total }' out 

목록이 출력되고 합계는 STDOUT으로 보내집니다.

grep -c -r 'import' Python-2.5.4/ > out && \ 
    awk -F : '{total += $2} END { print "Total:", total }' out 
Total: 11500 

$ head out 
Python-2.5.4/Python/import.c:155 
Python-2.5.4/Python/thread.o:0 
Python-2.5.4/Python/pyarena.c:0 
Python-2.5.4/Python/getargs.c:0 
Python-2.5.4/Python/thread_solaris.h:0 
Python-2.5.4/Python/dup2.c:0 
Python-2.5.4/Python/getplatform.c:0 
Python-2.5.4/Python/frozenmain.c:0 
Python-2.5.4/Python/pyfpe.c:0 
Python-2.5.4/Python/getmtime.c:0 

그냥 '문자열'의 발생과 선을 얻고 싶은 경우에, 변경이에 : 여기

는 Python2.5.4 디렉토리 트리에 출력

grep -c -r 'import' Python-2.5.4/ | \ 
    awk -F : '{total += $2; print $1, $2} END { print "Total:", total }' 

그 것이다 출력 :

[... snipped] 
Python-2.5.4/Lib/dis.py 4 
Python-2.5.4/Lib/mhlib.py 10 
Python-2.5.4/Lib/decimal.py 8 
Python-2.5.4/Lib/new.py 6 
Python-2.5.4/Lib/stringold.py 3 
Total: 11500 

파일 ($ 1) 및 파일 당 개수 ($ 2)를 인쇄하는 방법을 변경할 수 있습니다. 출력하려면

+0

어디에서 텍스트 확장자를 할당합니까? – Codex73

+0

죄송합니다. 이전 편집에서 온 것입니다. 확장 기능은 너무나 어둡습니다 :-) –

+0

문자열이있는 파일에만 어떻게 출력 할 수 있습니까? – Codex73

1

나는 find와 grep의 조합을 시도 할 것이다.

find . | xargs grep -c "string here" 

어쨌든, 나를 위해 grep -c -r "string here" * 작업 (맥 OS X).

+0

문자열이 너무 커서 xargs가 작동하지 않습니다. – Codex73

+0

공백을주의하십시오. -print0 및 -0을 고려하십시오. –

9

Bash의 프로세스 대체를 사용하면 원하는 결과물을 얻을 수 있습니다. (그렇지 않은 경우 문제를 명확히하십시오.)

grep -r "string here" * | tee >(wc -l) 

이 출력은 표준 출력과 wc -l 과정을 모두가는 일반적으로 grep -r를 실행합니다.

0

에만 일치와 파일 이름을가 사용

그것은 표현이 검색 일치하는 각 파일의 파일 이름으로 출력 한 줄 것
grep -r -l "your string here" . 

.

2

AWK 일부 용액 :

grep -r "string here" * | awk 'END { print NR } 1' 

다음 하나는 모든 일치를 표시하는 각 (최초로 일치 표시의 총 수, 파일 수, 각각의 일치의 수는이며,의 조건을 변경 ++f[$1]) : "boost::"에 대한 디렉토리 내에서 검색하는 최초의 솔루션 (대한

grep -r "string here" * | 
    awk -F: 'END { print "\nmatches: ", NR, "files: ", length(f); 
        for (i in f) print i, f[i] } !f[$1]++' 

출력.가) 수평으로 맞 그래서 수동으로 일부 너무 긴 줄을 잘라 : 결과에서 두 번째

list_inserter.hpp:   return range(boost::begin(r), boost::end(r)); 
list_of.hpp:   ::boost::is_array<T>, 
ptr_list_of.hpp:       BOOST_DEDUCED_TYPENAME boost::ptr_... 

matches: 13 files: 3 
ptr_list_of.hpp 2 
list_of.hpp 10 
list_inserter.hpp 1 

색상에 대한

list_inserter.hpp:   return range(boost::begin(r), boost::end(r)); 
list_of.hpp:   ::boost::is_array<T>, 
list_of.hpp:   ::boost::decay<const T>, 
list_of.hpp:   ::boost::decay<T> >::type type; 
list_of.hpp:  return ::boost::iterator_range_detail::equal(l, r); 
list_of.hpp:  return ::boost::iterator_range_detail::less_than(l, r); 
list_of.hpp:  return ::boost::iterator_range_detail::less_than(l, r); 
list_of.hpp:  return Os << ::boost::make_iterator_range(r.begin(), r.end()); 
list_of.hpp:   return range(boost::begin(r), boost::end(r)); 
list_of.hpp:   return range(boost::begin(r), boost::end(r)); 
list_of.hpp:   return range(boost::begin(r), boost::end(r)); 
ptr_list_of.hpp:       BOOST_DEDUCED_TYPENAME boost::ptr_... 
ptr_list_of.hpp:  typedef boost::ptr_vector<T>  impl_type; 
13 

출력) --color=always 그렙에 (좋은,하지만 통해 파이프 때 휴식 여기 awk. 그래서 당신이 나중에 당신의 모든 터미널을 채색하고 싶지 않다면 더 좋게하지 마십시오. :) 건배!

1
grep -rc "my string" ./ | grep :[1-9] >> file_name_by_count.txt 

매력처럼 작동합니다.