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

查看:55
本文介绍了用 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天全站免登陆