免费注册 查看新帖 |

Chinaunix

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

简单shell 脚本就是看不懂,晕。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-14 11:42 |只看该作者 |倒序浏览
  1. #!/bin/sh
  2. # grep3 - prints out three lines around pattern
  3. # if there is only one argument, exit

  4. case $# in
  5.         1);;
  6.         *) echo "Usage: $0 pattern";exit;;
  7. esac;
  8. # I hope the argument doesn't contain a /
  9. # if it does, sed will complain

  10. # use sed -n to disable printing
  11. # unless we ask for it
  12. sed -n '
  13. '/$1/' !{
  14.         #no match - put the current line in the hold buffer
  15.         x
  16.         # delete the old one, which is
  17.         # now in the pattern buffer
  18.         d
  19. }
  20. '/$1/' {
  21.         # a match - get last line
  22.         x
  23.         # print it
  24.         p
  25.         # get the original line back
  26.         x
  27.         # print it
  28.         p
  29.         # get the next line
  30.         n
  31.         # print it
  32.         p
  33.         # now add three dashes as a marker
  34.         a\
  35. ---
  36.         # now put this line into the hold buffer
  37.         x
  38. }
复制代码
'

前边的case 就是bug 呀。后边的sed 没有理解。

论坛徽章:
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
2 [报告]
发表于 2012-07-14 12:07 |只看该作者
grep -C3

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
3 [报告]
发表于 2012-07-14 12:38 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP