[grep] grep은 검색 패턴과 일치하는 단어 만 표시 할 수 있습니까?

검색 표현식과 일치하는 파일에서 grep 출력을 “단어”로 만드는 방법이 있습니까?

여러 파일에서 “th”와 같은 모든 인스턴스를 찾으려면 다음을 수행 할 수 있습니다.

grep "th" *

그러나 출력은 다음과 같습니다 (굵게 나타납니다).

일부 텍스트 파일은 : 고양이에 앉아 매트  
일부-다른 텍스트 파일 : 빠른 갈색 여우  
아직 다른 텍스트 파일 : 나는 희망  그것을 설명 철저

동일한 검색을 사용하여 출력하고 싶은 것은 다음과 같습니다.

the
the
the
this
thoroughly

grep을 사용하여 가능합니까? 아니면 다른 도구 조합을 사용합니까?



답변

grep -o를 사용해보십시오

grep -oh "\w*th\w*" *

편집 : Phil의 의견과 일치

에서 워드 프로세서 :

-h, --no-filename
    Suppress the prefixing of file names on output. This is the default
    when there is only  one  file  (or only standard input) to search.
-o, --only-matching
    Print  only  the matched (non-empty) parts of a matching line,
    with each such part on a separate output line.


답변

교차 배포 안전 답변 (Windows minGW 포함)

grep -h "[[:alpha:]]*th[[:alpha:]]*" 'filename' | tr ' ' '\n' | grep -h "[[:alpha:]]*th[[:alpha:]]*"

-o 옵션을 포함하지 않는 이전 버전의 grep (예 : 2.4.2)을 사용하는 경우 위를 사용하십시오. 그렇지 않으면 더 간단한 버전을 유지하십시오.

리눅스 교차 배포 안전 답변

grep -oh "[[:alpha:]]*th[[:alpha:]]*" 'filename'

-oh출력 을 요약 하면 정규 표현식이 vim / etc에서 작동하는 방식과 마찬가지로 파일 이름이 아닌 파일 내용과 일치하는 정규 표현식이 검색됩니다. 당신! 펄 구문이 아닌 POSIX를 유지하는 한 (아래 참조)

grep 매뉴얼에서 더보기

-o      Print each match, but only the match, not the entire line.
-h      Never print filename headers (i.e. filenames) with output lines.
-w      The expression is searched for as a word (as if surrounded by
         `[[:<:]]' and `[[:>:]]';

원래 답변이 모든 사람에게 적용되지 않는 이유

\w확장 된 “perl”구문으로 사용법은 플랫폼마다 다릅니다. 따라서 POSIX 문자 클래스와 함께 작동하도록 제한되는 grep 설치 [[:alpha:]]는 펄에 해당하지 않습니다 \w. 자세한 내용은 정규식 Wikipedia 페이지를 참조하십시오.

궁극적으로 위의 POSIX 답변은 grep 플랫폼 (원본)에 관계없이 훨씬 안정적입니다.

-o 옵션없이 grep을 지원하는 경우 첫 번째 grep은 관련 행을 출력하고 tr은 공백을 새 행으로 분할하고 최종 grep은 해당 행에 대해서만 필터링합니다.

(PS : 지금까지 대부분의 플랫폼을 알고 있으며 \ w …에 대해 패치되었을 것입니다.

@AdamRosenfield 답변의 “-o”해결 방법에 대한 크레딧


답변

생각보다 간단합니다. 이 시도:

egrep -wo 'th.[a-z]*' filename.txt #### (Case Sensitive)

egrep -iwo 'th.[a-z]*' filename.txt  ### (Case Insensitive)

어디,

 egrep: Grep will work with extended regular expression.
 w    : Matches only word/words instead of substring.
 o    : Display only matched pattern instead of whole line.
 i    : If u want to ignore case sensitivity.


답변

다음과 같이 공백을 개행으로 변환 한 다음 grep 할 수 있습니다.

cat * | tr ' ' '\n' | grep th


답변

단지 awk도구 조합이 필요하지 않습니다.

# awk '{for(i=1;i<=NF;i++){if($i~/^th/){print $i}}}' file
the
the
the
this
thoroughly


답변

일치 및 perl 전용 grep 명령

grep -o -P 'th.*? ' filename


답변

나는 awk의 구문을 기억하기 어려운 것에 만족하지 않았지만 이것을 수행하기 위해 하나의 유틸리티를 사용하는 아이디어를 좋아했다.

ack (또는 Ubuntu를 사용하는 경우 ack-grep)이 쉽게 할 수있는 것처럼 보입니다.

# ack-grep -ho "\bth.*?\b" *

the
the
the
this
thoroughly

-h 플래그를 생략하면 다음과 같은 결과가 나타납니다.

# ack-grep -o "\bth.*?\b" *

some-other-text-file
1:the

some-text-file
1:the
the

yet-another-text-file
1:this
thoroughly

보너스로, --output플래그를 사용하여 내가 찾은 가장 쉬운 구문으로 더 복잡한 검색을 수행 할 수 있습니다 .

# echo "bug: 1, id: 5, time: 12/27/2010" > test-file
# ack-grep -ho "bug: (\d*), id: (\d*), time: (.*)" --output '$1, $2, $3' test-file

1, 5, 12/27/2010