Chinaunix

标题: sed 匹配行内出现n次 [打印本页]

作者: dreamone503    时间: 2008-11-12 13:03
标题: sed 匹配行内出现n次
aaa  bbb  ccc ddd aaa mmm  nnn
aaa  bbb  ccc ddd eee www   yyy
aaa  kkk  lll  sss  rrr  aaa    aaa



如何匹配出现3次aaa的行 和 出现 1次aaa的行?
作者: ynchnluiti    时间: 2008-11-12 13:14
sed -nr '/(.*aaa.*){3}/p'
作者: waker    时间: 2008-11-12 13:14
sed  '/aaa/{s//&/4;tb;s//&/1;t;s//&/3;t}
:b;d' urfile
作者: RomanHeart    时间: 2008-11-12 13:28
标题: sed '/.a\{3\}/p'
sed '/.a\{3\}/p'
作者: dreamone503    时间: 2008-11-12 13:28
标题: 回复 #3 waker 的帖子
汗, waker,  这个sed怎么复杂, 真的不能在简单了么?
作者: dreamone503    时间: 2008-11-13 10:30
标题: 回复 #3 waker 的帖子
waker 解释一下可行, 没整明白阿
作者: ly5066113    时间: 2008-11-13 10:39
应该是这样吧:

sed  '/aaa/{s//&/4;tb;s//&/3;t;s//&/2;tb;p}   
:b;d' urfile
作者: baoyu05    时间: 2008-11-13 10:53
标题: 回复 #2 ynchnluiti 的帖子
如何只匹配两次出现的呢?
sed -nr '/(.*aaa.*){2}/p' file 这样的话把3次的也匹配了。
作者: ynchnluiti    时间: 2008-11-13 11:05
原帖由 baoyu05 于 2008-11-13 10:53 发表
如何只匹配两次出现的呢?
sed -nr '/(.*aaa.*){2}/p' file 这样的话把3次的也匹配了。

上边有
  1. sed -n '/aaa/{s//&/3;tb;s//&/2;Tb;p}
  2. :b;d' urfile
复制代码

作者: dreamone503    时间: 2008-11-13 13:45
标题: 回复 #9 ynchnluiti 的帖子
诸位大侠, 给解释下含义被?
没用过tb  Tb阿, 而且另起一行的:b;d是什么意思阿?

sed -n '/aaa/{s//&/3;tb;s//&/2;Tb;p}
:b;d' urfile
作者: ynchnluiti    时间: 2008-11-13 13:50
原帖由 dreamone503 于 2008-11-13 13:45 发表
诸位大侠, 给解释下含义被?
没用过tb  Tb阿, 而且另起一行的:b;d是什么意思阿?

sed -n '/aaa/{s//&/3;tb;s//&/2;Tb;p}
:b;d' urfile

man sed
       : label
              Label for b and t commands.
       t label
              If a s/// has done a successful substitution since the last input line was read and since the last t or T command, then branch to label; if
              label is omitted, branch to end of script.
       T label
              If no s/// has done a successful substitution since the last input line was read and since the last t or T command, then branch  to  label;
              if label is omitted, branch to end of script.

:b;定义标签b
tb;是上一次s///成功则跳到b;否则继续执行。
Tb;跟tb相反,替换不成功跳转
作者: yezhulin1982    时间: 2008-11-13 14:05
学习了!
作者: dreamone503    时间: 2008-11-14 11:45
标题: 回复 #11 ynchnluiti 的帖子
领教领教,谢谢诸位

这样运行可以, sed -n '/aaa/{s//&/3;tb;s//&/2;Tb;p};:b;d' urfile

不知为何下边句子中 :b;d 要单起一行?
sed -n '/aaa/{s//&/3;tb;s//&/2;Tb;p}
:b;d' urfile
作者: ynchnluiti    时间: 2008-11-14 12:05
原帖由 dreamone503 于 2008-11-14 11:45 发表
领教领教,谢谢诸位

这样运行可以, sed -n '/aaa/{s//&/3;tb;s//&/2;Tb;p};:b;d' urfile

不知为何下边句子中 :b;d 要单起一行?
sed -n '/aaa/{s//&/3;tb;s//&/2;Tb;p}
:b;d' urfile

都可以,一个是分号,一个是回车。都可以分割命令。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2