免费注册 查看新帖 |

Chinaunix

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

grep的妙用 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-18 10:07 |只看该作者 |倒序浏览
本帖最后由 defcon 于 2012-04-19 08:34 编辑

大牛勿喷,菜鸟可以掌握掌握,少走弯路


grep 'root' /etc/passwd >/dev/null 2>&1

if [ $? -eq 0 ]  

then
.....

你还在用这样的判断方式吗?,那你是没有好好看grep的帮助文档

-q, --quiet, --silent

              Quiet;  do  not  write  anything  to  standard   output.    Exit

              immediately  with  zero status if any match is found, even if an

              error was detected.  Also see the -s  or  --no-messages  option.

              (-q is specified by POSIX.)

其实一个-q 参数就可以静默的输入。以后记得用这个啦
     
   
grep -q 'root' /etc/passwd            
   
if [ $? -eq 0 ]
   
then  
  
.....     


还有一个文件就是你还在sed或者其他方式来查找指定的字符所在行的上下N行吗?
那你又out了,看man手册

      -A NUM, --after-context=NUM
             Print  NUM  lines  of  trailing  context  after  matching lines.
             Places  a  line  containing  a  group  separator  (--)   between
             contiguous  groups  of  matches.  With the -o or --only-matching
             option, this has no effect and a warning is given.

      -B NUM, --before-context=NUM
             Print NUM  lines  of  leading  context  before  matching  lines.
             Places   a  line  containing  a  group  separator  (--)  between
             contiguous groups of matches.  With the  -o  or  --only-matching
             option, this has no effect and a warning is given.

      -C NUM, -NUM, --context=NUM
             Print  NUM  lines of output context.  Places a line containing a
             group separator (--) between contiguous groups of matches.  With
             the  -o  or  --only-matching  option,  this  has no effect and a
             warning is given.  
例子:
[root@shanker ~]# seq 10 |grep 5 -A3
5
6
7
8
[root@shanker ~]# seq 10|grep 5 -B3
2
3
4
5
[root@shanker ~]# seq 10|grep 5 -C3
2
3
4
5
6
7
8
很容易理解吧

论坛徽章:
0
2 [报告]
发表于 2012-04-18 10:42 |只看该作者
学习了,一直不知道-q的参数

论坛徽章:
3
水瓶座
日期:2014-03-25 17:08:042015亚冠之塔什干棉农
日期:2015-08-10 10:45:122015亚冠之萨济拖拉机
日期:2015-08-13 16:05:24
3 [报告]
发表于 2012-04-18 14:02 |只看该作者
学习了

论坛徽章:
4
技术图书徽章
日期:2013-09-23 10:22:37狮子座
日期:2013-10-15 23:31:54卯兔
日期:2013-11-11 17:33:15金牛座
日期:2013-11-15 17:25:28
4 [报告]
发表于 2012-04-18 17:16 来自手机 |只看该作者
是 $? -eq 0
手机上找不到中括号
你可以grep -q root /etc/passwd && command

论坛徽章:
0
5 [报告]
发表于 2012-04-18 18:53 |只看该作者
回复 4# nbrr


    这么牛,居然用的是手机访问论坛,得向你学习啊

论坛徽章:
4
技术图书徽章
日期:2013-09-23 10:22:37狮子座
日期:2013-10-15 23:31:54卯兔
日期:2013-11-11 17:33:15金牛座
日期:2013-11-15 17:25:28
6 [报告]
发表于 2012-04-18 19:50 |只看该作者
-q 只要找到第一个匹配,就会返回状态0
如果不加-q, grep会扫描整个文件
要判断状态,应该是

  1. if [ $? -eq 0 ]
  2. then
  3.         commond1;
  4.         commond2;
  5.         commond3;
  6. fi
复制代码
也可以这样写

  1. if grep -q root /etc/passwd
  2. then
  3.         commond1;
  4.         commond2;
  5.         commond3;
  6. fi
复制代码
如果只有一个command就可以

  1. grep -q root /etc/passwd && command
复制代码

论坛徽章:
0
7 [报告]
发表于 2012-04-18 20:40 |只看该作者
能做就行了,何况不同版本不同平台,不一定都有

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
8 [报告]
发表于 2012-04-18 20:57 |只看该作者
回复 7# k123w456


    是的,UNIX系列的大部分没有-A, -B, -C等

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2012-04-19 08:33 |只看该作者
回复 6# nbrr


    学习了,谢谢

论坛徽章:
0
10 [报告]
发表于 2012-04-19 21:05 |只看该作者
受教了,命令参数太多了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP