用grep在一行中匹配两个字符串 [英] Match two strings in one line with grep

查看:389
本文介绍了用grep在一行中匹配两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用grep来匹配包含两个不同字符串的行.我已经尝试了以下操作,但这与包含 string1 string2 的行匹配,这不是我想要的.

I am trying to use grep to match lines that contain two different strings. I have tried the following but this matches lines that contain either string1 or string2 which not what I want.

grep 'string1\|string2' filename

那么我如何只将同时包含两个字符串的行与grep匹配?

So how do I match with grep only the lines that contain both strings?

推荐答案

您可以使用grep 'string1' filename | grep 'string2'

或者,grep 'string1.*string2\|string2.*string1' filename

这篇关于用grep在一行中匹配两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆