regex - How to grep regex1 OR regex2?
问题描述
I would like to grep (on ubuntu18.04) a every line that fits the pattern that "at least one of two number is greater than 0".
I found infomation about '|' usage in regex but I see it does not work here.
One of tried option:
grep -E "Foo:[1-9]+ | Bar:[1-9]+" input
which returns:
Foo:1, Bar:1
Foo:0, Bar:1
and that is invalid cause it matches only Bar:[1-9]+
Input data:
Foo:1, Bar:0
Foo:1, Bar:1
Foo:0, Bar:1
Foo:0, Bar:0
Foo:55, Bar:0
Expected result in a solution:
Foo:1, Bar:0
Foo:1, Bar:1
Foo:0, Bar:1
Foo:55, Bar:0
解决方案
The problem is the space after the number: there's no such space in your input data.
grep -E 'Foo:[1-9]|Bar:[1-9]'
gives the expected output.
It can be further simplified into
grep -E '(Foo|Bar):[1-9]'
Note that the +
isn't needed: a number starting with 1-9 is already greater than 0 even if it's followed by 0 or by nothing at all.
推荐阅读
- angular - ngx-loading 未通过 Angular 5 中的服务加载
- spring - 在事务之外访问数据的模式
- javascript - 如何在 Angular 4 中调用 httpclient 的 get 方法从服务器检索数据?
- sql - 为每一行选择随机值
- python-2.7 - 为 pyspark 烧瓶应用程序中的每个请求创建 Spark 上下文
- javascript - 如何将javascript [“a”,“b”,“c”,“d”]中的数组转换为[{“a”,“c”},{“b,d”}]?
- java - LinkedHashMap 中的不同错误 - java8.1
- android - 将数据从 Firebase 提取到 RecyclerView 时出错。致命异常:...没有定义无参数构造函数
- php - php项目的网络摄像头访问
- python - 如何在循环中从字典中填充数据框