免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6312 | 回复: 18
打印 上一主题 下一主题

请教:grep怎么设置多条件查询 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-10 11:43 |只看该作者 |倒序浏览
li@mail test]# cat gt
one two three
four five six
one two
one seven three

[li@mail test]# grep one three gt
grep: three: No such file or directory
gtne two three
gtne two
gtne seven three

我的原意是:截取出现one 和 three的行,结果应该是:
one two three
one seven three

能不能不用管道的方式实现( cat gt | grep one | grep three ) 而直接用一个grep命令?

论坛徽章:
0
2 [报告]
发表于 2004-05-10 11:46 |只看该作者

请教:grep怎么设置多条件查询

用管道,grep one gt|grep three

论坛徽章:
0
3 [报告]
发表于 2004-05-10 11:47 |只看该作者

请教:grep怎么设置多条件查询

use awk:
  1. awk '/one/&&/three/'
复制代码

论坛徽章:
0
4 [报告]
发表于 2004-05-10 12:19 |只看该作者

请教:grep怎么设置多条件查询

[quote]原帖由 "bjgirl"][/quote 发表:


谢谢了, awk不错, 能不能用sed实现呢?

论坛徽章:
0
5 [报告]
发表于 2004-05-10 12:40 |只看该作者

请教:grep怎么设置多条件查询

if syntax is fixed,try it
  1. sed -n '/one .* three/p'
复制代码

论坛徽章:
0
6 [报告]
发表于 2004-05-10 12:44 |只看该作者

请教:grep怎么设置多条件查询

[quote]原帖由 "bjgirl"][/quote 发表:


谢谢, bjgirl真热心
这样只是针对“one” 在前, “three”在后的情况吧,
如果次序不限制呢?

论坛徽章:
0
7 [报告]
发表于 2004-05-10 12:58 |只看该作者

请教:grep怎么设置多条件查询

原帖由 "paulwang" 发表:


谢谢, bjgirl真热心
这样只是针对“one” 在前, “three”在后的情况吧,
如果次序不限制呢?

so,awk is best choice example:
tme:
one two three
four five six
one two
one seven three
one abc ddd three ddd
df one three
three one dddd

  1. cat tmp|awk '/one/&&/three/'
  2. one two three
  3. one seven three
  4. one abc ddd three ddd
  5. df one three
  6. three one dddd
复制代码

论坛徽章:
0
8 [报告]
发表于 2004-05-10 13:21 |只看该作者

请教:grep怎么设置多条件查询

谢谢, 这两天在学习shell,发现越来越好玩,
shell版有很多象bjgirl这么热心的人, 在你们的帮助和带动下面, 使shell新手很容易入门和产生兴趣。

非常感谢

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
9 [报告]
发表于 2004-05-10 16:04 |只看该作者

请教:grep怎么设置多条件查询

  1. sed -ne '/one/{/three/p}'  
复制代码

论坛徽章:
0
10 [报告]
发表于 2004-05-10 17:00 |只看该作者

请教:grep怎么设置多条件查询

好,
又学了一着
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP