2017-05-14 5 views
1

파일에서 여러 문자열을 찾는 방법은 모든 문자열이 grep Linux를 사용하여 파일에있는 경우 을 반환해야합니다.grep을 사용하여 파일에서 여러 문자열 찾기

+1

에 오신 것을 환영합니다. 샘플 데이터를 예상 출력과 함께 표시하고 문제 해결을 위해 노력하십시오. –

답변

0

이 시도 :

if grep -q string1 filename && grep -q string2 filename; then 
    echo 'True' 
else 
echo 'false' 
fi 

테스트 코드 조각을 : AWK의

Test Output

2

파일에서 다중 문자열을 검색하려면 리눅스에서 egrep 또는 grep을 사용할 수 있습니다.

egrep -ri --color 'string1|string2|string3' /path/to/file 

-r search recursively 
-i ignore case 
--color - displays the search matches with color 

그런 다음 echo $? grep 명령이 더 일치 여기에서

$? is a variable holding the return value of the last command you ran. 

당신이 떠들썩한 파티 플레이와 당신이 필요로하는 작은 스크립트 또는 무엇이든을 만들 수 없었다 경우 그렙 아무것도 일치 1 (false)를 경우하는 0 (참)이 표시됩니다.

0

하나. 먼저 테스트 파일 :

$ cat file 
foo 
bar 
baz 

코드 및 테스트 실행 :

$ awk ' 
BEGIN { 
    RS="\177"        # set something unusual to RS and append 
    FS=FS "\n" }       # \n to FS to make the whole file one record 
{ 
    print (/foo/&&/bar/?"true":"false") } # search and output true or false 
    # exit (/foo/&&/bar/?0:1)    # exit if you are interested in return value 
' file 
true 

한 - 라이너 : 유래에

$ awk 'BEGIN{RS="\177";FS=FS "\n"} {print (/foo/&&/bar/?"true":"false")}' file 
관련 문제