如何在shell脚本中使用grep查找此模式?

时间:2014-06-23 10:01:22

标签: regex grep sh

我如何在grep中找到这种模式?

1.1.1.1 (everything) testString (everything)

我的意思是我需要一个搜索,可以找出每种情况下是否存在testString:

1.1.1.1 test1 testString test2
1.1.1.1 test1 test2 testString
1.1.1.1 testString test1 test2

然后,为了将结果传递给shell sh脚本中的if子句,我如何以“false”(未找到)或“true”(找到它)的形式获取grep的结果?

1 个答案:

答案 0 :(得分:0)

你的问题不是很清楚。您在寻找testString1.1.1.1后跟testString吗?

假设第二种情况,并假设您正在从文件中读入:

while read line; do
  echo $line | grep -q '1\.1\.1\.1.*testString' && echo found || echo not found
done < filename.txt