grep으로 온전히 일치하는 단어만 검색하기 질문

kkb의 이미지

vi에서 온전히 일치하는 단어만 검색하려면

/\<apple\>

위와 같이 검색하면 온전히 일치하는 단어만 검색되잖아요?

This is a apple juice. 검색되고
ret=apple(2, 3.14159); 검색되고
ice->juice->apple++; 검색되고
sum=pine+apple-2; 검색됩니다.

반면에,
apple_juice 검색되지 않고
applemango 검색되지 않고
2apple 검색되지 않죠.

/apple

위와 같이 검색해야 위의 모든 문장들이 검색되겠죠.

이처럼 grep에서도 온전히 일치하는 단어만 검색하는 방법이 있나요?

Hyun의 이미지

아래와 같이 하면 되지 않을까요?

[hyun@ ~]$ echo -e "apple\napple_juice" | grep '\<apple\>'
apple
[hyun@ ~]$ echo -e "apple\napple_juice" | grep 'apple'
apple
apple_juice
[hyun@ ~]$ 
김정균의 이미지

똑같이 하면 되는데,, 왜 안해 보셨나요?

[somebody@AL01514686 ~]$ cat <<EOL | grep '\<apple\>'
> This is a apple juice. 검색되고
> ret=apple(2, 3.14159); 검색되고
> ice->juice->apple++; 검색되고
> sum=pine+apple-2; 검색됩니다.
>
> 반면에,
> apple_juice 검색되지 않고
> applemango 검색되지 않고
> 2apple 검색되지 않죠.
> EOL
This is a apple juice. 검색되고
ret=apple(2, 3.14159); 검색되고
ice->juice->apple++; 검색되고
sum=pine+apple-2; 검색됩니다.
kkb의 이미지

아 분명히 한번 해보고 안되길래 질문 올린 건데;;
이상하네요
죄송합니다

답변 주신 분들 모두 감사드립니다

익명 사용자의 이미지

$ man grep
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.