【发布时间】:2016-06-03 19:39:20
【问题描述】:
我想检查 bash 中是否存在文件中的多行。
所以我使用grep -q,它只适用于一行:
if grep -q string1 "/path/to/file";then
echo 'exists'
else
echo 'does not exist'
fi
我尝试了很多不同的组合,例如:
if grep -q [ string1 ] && grep -q [ string2 ] "path/to/file";then
我也试过-E:
grep -E 'pattern1' filename | grep -E 'pattern2'
但似乎没有任何效果。有什么想法吗?
【问题讨论】:
-
grep -q string_1 file && grep -q string_2 file? -
@iruvar 将其作为答案发布
-
这个我也试过了,但是脚本永远不会完成
-
@PaulBernhardWagner,这可能是因为您错过了在
string1之后指定file? -
天哪,我觉得自己太笨了,这行得通。但这意味着在 bash 中不可能写得比这更简单?就像在其他语言中你只有
if (cond1 && cond2) ....