2017-05-14 20 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