your programing

grep, 특정 파일 확장자 만

lovepro 2020. 9. 28. 09:48
반응형

grep, 특정 파일 확장자 만


grep특정 디렉토리에 일부 스크립트를 작성하는 중이지만 이러한 디렉토리에는 모든 종류의 파일 유형이 포함되어 있습니다.

내가 원하는 grep단지 .h.cpp지금,하지만 미래에 어쩌면 몇 가지 다른.

지금까지 :

{ grep -r -i CP_Image ~/path1/;

grep -r -i CP_Image ~/path2/;

grep -r -i CP_Image ~/path3/;

grep -r -i CP_Image ~/path4/;

grep -r -i CP_Image ~/path5/;} 

| mailx -s GREP email@domain.com

누구든지 특정 파일 확장자 만 추가하는 방법을 보여줄 수 있습니까?


다음 --include과 같이 매개 변수를 사용하십시오 .

grep -r -i --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.com

그것은 당신이 원하는 것을해야합니다.

구문 참고 :

  • -r -재귀 적으로 검색
  • -i-대소 문자를 구분하지 않는 검색
  • --include=\*.${file_extension} -확장자 또는 파일 패턴과 일치하는 파일 만 검색

이 답변 중 일부는 구문이 너무 무거워 보이거나 데비안 서버에 문제가 발생했습니다. 이것은 나를 위해 완벽하게 작동했습니다.

PHP 혁명 : Linux에서 파일을 Grep하는 방법은 있지만 특정 파일 확장자 만 가능합니까?

즉:

grep -r --include=\*.txt 'searchterm' ./

... 또는 대소 문자를 구분하지 않는 버전 ...

grep -r -i --include=\*.txt 'searchterm' ./
  • grep: 명령

  • -r: 재귀 적으로

  • -i: 케이스 무시

  • --include: 모든 * .txt : 텍스트 파일 (파일 이름에 별표가있는 디렉토리가있는 경우 \로 이스케이프)

  • 'searchterm': 검색 대상

  • ./: 현재 디렉토리에서 시작합니다.


어때 :

find . -name '*.h' -o -name '*.cpp' -exec grep "CP_Image" {} \; -print

grep -rnw "some thing to grep" --include=*.{module,inc,php,js,css,html,htm} ./

HP 및 Sun 서버에는 -r 옵션이 없습니다.이 방법은 내 HP 서버에서 저에게 효과적이었습니다.

find . -name "*.c" | xargs grep -i "my great text"

-i는 대소 문자를 구분하지 않는 문자열 검색입니다.


이것은 파일 찾는 문제이므로 find!

GNU find를 사용하면 -regex확장자가 .h또는 인 디렉토리 트리에서 해당 파일을 찾는 옵션을 사용할 수 있습니다 .cpp.

find -type f -regex ".*\.\(h\|cpp\)"
#            ^^^^^^^^^^^^^^^^^^^^^^^

그런 다음 grep각 결과에 대해 실행하는 문제입니다 .

find -type f -regex ".*\.\(h\|cpp\)" -exec grep "your pattern" {} +

If you don't have this distribution of find you have to use an approach like Amir Afghani's, using -o to concatenate options (the name is either ending with .h or with .cpp):

find -type f \( -name '*.h' -o -name '*.cpp' \) -exec grep "your pattern" {} +
#            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And if you really want to use grep, follow the syntax indicated to --include:

grep "your pattern" -r --include=*.{cpp,h}
#                      ^^^^^^^^^^^^^^^^^^^

The easiest way is

find . -type  f -name '*.extension' | xargs grep -i string 

Below answer is good.

grep -r -i --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.com

But can be updated to:

grep -r -i --include \*.{h,cpp} CP_Image ~/path[12345] | mailx -s GREP email@domain.com

Which can be more simple.


I am aware this question is a bit dated, but I would like to share the method I normally use to find .c and .h files:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -H "#include"

or if you need the line number as well:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -nH "#include"

ag (the silver searcher) has pretty simple syntax for this

       -G --file-search-regex PATTERN
          Only search files whose names match PATTERN.

so

ag -G *.h -G *.cpp CP_Image <path>

Should write "-exec grep " for each "-o -name "

find . -name '*.h' -exec grep -Hn "CP_Image" {} \; -o -name '*.cpp' -exec grep -Hn "CP_Image" {} \;

Or group them by ( )

find . \( -name '*.h' -o -name '*.cpp' \) -exec grep -Hn "CP_Image" {} \;

option '-Hn' show the file name and line.

참고URL : https://stackoverflow.com/questions/12516937/grep-but-only-certain-file-extensions

반응형