免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: mr_caoke
打印 上一主题 下一主题

问一个有关sed匹配模式的话题 [复制链接]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
61 [报告]
发表于 2010-04-12 22:04 |只看该作者
再看一个简单的例子:
$ nl -b a urfile
     1  foo
     2  123
     3  abc
     4  bar
     5  xx
     6  xx
     7  foo
     8  123
     9  abc
    10  bar
    11  xx
    12  foo
    13  123
    14  abc

$ nl -b a urfile|sed -n '/foo/,/bar/p'
     1  foo
     2  123
     3  abc
     4  bar
     7  foo
     8  123
     9  abc
    10  bar
    12  foo
    13  123
    14  abc

论坛徽章:
0
62 [报告]
发表于 2010-04-12 22:09 |只看该作者
刚才碰到一个问题,才发帖 你看看。 为什么要远离shell呢?

论坛徽章:
0
63 [报告]
发表于 2010-04-12 22:10 |只看该作者
用的是AIX 系统;  就是这个网站提供的  http://www.unix-center.net/
AIX 服务器地址为: aix.unix-center.net  
黑哥肯定有账号的啦, 这个是你介绍的网站

sy@/home/t/a/crowsy/sh>cat -A txt
1 an$
2 an$
3 an$
4$
5 an$
sy@/home/t/a/crowsy/sh>sed -n '/an/,2p' txt
1 an
2 an
5 an
sy@/home/t/a/crowsy/sh>uname -a
AIX aix 3 5 00C97AC04C00 powerpc unknown AIX
sy@/home/t/a/crowsy/sh>sed --version
sed: Not a recognized flag: -
Usage:  sed [-n] Script [File ...]
        sed [-n] [-e Script] ... [-f Script_file] ... [File ...]
sy@/home/t/a/crowsy/sh>

论坛徽章:
0
64 [报告]
发表于 2010-04-12 22:10 |只看该作者
回复 61# blackold


    谢谢,这回彻底明白啦

论坛徽章:
0
65 [报告]
发表于 2010-04-12 22:11 |只看该作者
回复  crowsy001


    是不是你的文件有问题呢?

   我这里是匹配第3行的。
blackold 发表于 2010-04-12 21:57


我感觉可能和系统有关系, 要不就是sed 有关系。  我一直用 AIX 系统。
    A command line with two addresses separated by commas selects the entire range from the first line that matches the
first address through the next line that matches the second. (If the second address is a number less than or equal to
the line number first selected, only one line is selected.) Thereafter, the process is repeated, looking again for the
first address.

论坛徽章:
0
66 [报告]
发表于 2010-04-12 22:28 |只看该作者
昏死    AIX 系统就会丢一行
man  sed  差异也很大
sy@/home/t/a/crowsy/sh>cat > txt
1 an
2 an
3 an
4
5 an
^C
sy@/home/t/a/crowsy/sh>sed -n '/an/,2p' txt
1 an
2 an
3 an
5 an
sy@/home/t/a/crowsy/sh>uname
FreeBSD
sy@/home/t/a/crowsy/sh>sed --version
sed: illegal option -- -
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
67 [报告]
发表于 2010-04-12 22:55 |只看该作者
回复 63# crowsy001


    刚才测试了。结果确实没有匹配第3行。原因待查。

论坛徽章:
0
68 [报告]
发表于 2010-04-13 13:53 |只看该作者
/add1/add2/

我的理解是,找到add1,再找add2;如果找不到add2,直到末尾。

论坛徽章:
0
69 [报告]
发表于 2010-04-13 17:32 |只看该作者
sed -n 'a,b p' filename

a b 可以是行号, 可以是模式匹配
找到a 开始打印,到b结束;再重新找a,打印,到b结束。。。直到文件尾

'3,7 p' 打印3到7行
'/str/,7 p' 找到str,一直print到第7行;接着找str,后面的行号大于7,只打印匹配str的当前行。

论坛徽章:
0
70 [报告]
发表于 2010-04-14 10:57 |只看该作者
说实在还是搞不明白
谁来个总结陈辞啊!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP