使用grep在文件中查找多个字符串
Find Multiple string in file using grep
如何在一个文件中查找多个字符串,当所有字符串都出现在使用 grep Linux.
文件
要在文件中搜索多个字符串,您可以在 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
你可以然后回显 $?如果你的 grep 匹配任何东西,它会显示 0(真),如果 grep 命令没有匹配,它会显示 1(假)
$? is a variable holding the return value of the last command you ran.
从这里您可以玩 bash 并创建一个小脚本或任何您需要的东西。
试试这个:
if grep -q string1 filename && grep -q string2 filename; then
echo 'True'
else
echo 'false'
fi
测试片段:
awk 中的一个。首先是测试文件:
$ cat file
foo
bar
baz
编码和测试运行:
$ awk '
BEGIN {
RS="7" # 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="7";FS=FS "\n"} {print (/foo/&&/bar/?"true":"false")}' file
如何在一个文件中查找多个字符串,当所有字符串都出现在使用 grep Linux.
文件要在文件中搜索多个字符串,您可以在 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
你可以然后回显 $?如果你的 grep 匹配任何东西,它会显示 0(真),如果 grep 命令没有匹配,它会显示 1(假)
$? is a variable holding the return value of the last command you ran.
从这里您可以玩 bash 并创建一个小脚本或任何您需要的东西。
试试这个:
if grep -q string1 filename && grep -q string2 filename; then
echo 'True'
else
echo 'false'
fi
测试片段:
awk 中的一个。首先是测试文件:
$ cat file
foo
bar
baz
编码和测试运行:
$ awk '
BEGIN {
RS="7" # 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="7";FS=FS "\n"} {print (/foo/&&/bar/?"true":"false")}' file