免费注册 查看新帖 |

Chinaunix

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

[文本处理] grep -m 在循环中使用异常 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-08-19 19:51 |只看该作者 |倒序浏览
  1.        -m NUM, --max-count=NUM
  2.               Stop reading a file after NUM matching lines.  If the input is  standard  input  from  a  regular  file,  and  NUM
  3.               matching lines are output, grep ensures that the standard input is positioned to just after the last matching line
  4.               before exiting, regardless of the presence of trailing context lines
复制代码
根据上面的帮助内容,grep在从文件流中获取指定匹配行后将文件指针定位到最后一此匹配行的后续行上
  1. # cat file
  2. slkd
  3. eioc
  4. weic
  5. eoc
  6. lweoijxlk
  7. skeoicj
  8. lksdoi
  9. # while  grep -m 2 "^[se]";do echo '-->find';done <file         <--- 经测试最后一次输出为什么有重复部分?如果"-m 1",会导致while陷入死循环,why?
  10. slkd
  11. eioc
  12. -->find
  13. eoc
  14. skeoicj
  15. -->find
  16. skeoicj
  17. -->find
复制代码
希望能有人答疑解惑?

论坛徽章:
6
摩羯座
日期:2013-08-24 10:43:10狮子座
日期:2013-08-25 10:27:06天秤座
日期:2013-09-11 20:28:44午马
日期:2014-09-28 16:06:0015-16赛季CBA联赛之八一
日期:2016-12-19 13:55:0515-16赛季CBA联赛之天津
日期:2016-12-20 14:01:23
2 [报告]
发表于 2013-08-19 20:23 |只看该作者
@chaogle
我的结果和你不一样
没有重复部分
-m 1 也不会导致while死循环。

论坛徽章:
0
3 [报告]
发表于 2013-08-19 20:57 |只看该作者
回复 2# cao627
是吗?
我的grep版本为:
  1. # grep -V
  2. GNU grep 2.6.3

  3. Copyright (C) 2009 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.
复制代码
死循环结果为:
  1. # while  grep -m 1 -e "^[e]" ;do echo '-->find';done<file
  2. eioc
  3. -->find
  4. eoc
  5. -->find
  6. eoc
  7. -->find
  8. eoc
  9. -->find
  10. eoc
  11. .
  12. .
  13. .
  14. .
复制代码

论坛徽章:
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
4 [报告]
发表于 2013-08-19 21:04 |只看该作者
什么操作系统?
  1. uname -a
复制代码
文件是从Windows里面复制过来的?
  1. dos2unix file
复制代码

论坛徽章:
6
摩羯座
日期:2013-08-24 10:43:10狮子座
日期:2013-08-25 10:27:06天秤座
日期:2013-09-11 20:28:44午马
日期:2014-09-28 16:06:0015-16赛季CBA联赛之八一
日期:2016-12-19 13:55:0515-16赛季CBA联赛之天津
日期:2016-12-20 14:01:23
5 [报告]
发表于 2013-08-19 21:10 |只看该作者
@chaogle
我试过了 redhat 上grep 也是2.6.3 是有这个问题。




论坛徽章:
6
摩羯座
日期:2013-08-24 10:43:10狮子座
日期:2013-08-25 10:27:06天秤座
日期:2013-09-11 20:28:44午马
日期:2014-09-28 16:06:0015-16赛季CBA联赛之八一
日期:2016-12-19 13:55:0515-16赛季CBA联赛之天津
日期:2016-12-20 14:01:23
6 [报告]
发表于 2013-08-19 21:13 |只看该作者
  1. $ uname -a
  2. Linux sun 3.8.0-21-generic #32-Ubuntu SMP Tue May 14 22:16:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
  3. $ grep -V
  4. grep (GNU grep) 2.14
  5. Copyright (C) 2012 Free Software Foundation, Inc.
  6. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
  7. This is free software: you are free to change and redistribute it.
  8. There is NO WARRANTY, to the extent permitted by law.

  9. Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
复制代码
我在这个系统上没问题。

论坛徽章:
0
7 [报告]
发表于 2013-08-19 21:50 |只看该作者
回复 6# cao627

Thanks for your answer.

我是在Rhel 6.4环境中使用grep的
看来这个属于版本问题了,我下载高版本的Grep试试

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP