2017-05-14 19 views
1

如何找到多個字符串在文件中查找文件多重字符串,其應該返回真正當所有的字符串存在於用grep Linux的文件。用grep

+1

歡迎#1。請顯示樣本數據以及預期的輸出結果以及您嘗試解決問題所做的一些努力。 –

回答

0

試試這個:

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

試驗段:

Test Output

2

要在文件中搜索多字符串,您可以在Linux上使用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 $?這將顯示0(真),如果你的grep匹配任何東西,1(假)如果grep命令的火柴

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

從這裏你可以使用bash播放和創建一個小腳本或任何你需要的。

0

一個在AWK。首先,測試文件:

$ 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