Chinaunix

标题: 请教各位大神,一个sed的问题 [打印本页]

作者: hyt123321c    时间: 2013-06-14 10:23
标题: 请教各位大神,一个sed的问题
现在有N个文件,文件需要匹配一串字符,然后删除匹配到的字符到最后一行。怎么操作?
比如
需要删除xx--#--xx (包括这行)到最后一行
file1:
abc
bcd
cdf
xx--#--xx
zxc
asd

file2
abc
bcd
xx--#--xx
zxd
asd
xx--#--xx
abc

file3
asd
bvc
xx--#--xx
rty

现在需要的结果是
file1:
abc
bcd
cdf


file2
abc
bcd


file3
asd
bvc

就是需要把
xx--#--xx 之后的内容都删除掉,如果操作?

作者: todayhero    时间: 2013-06-14 10:31
@hyt123321c
  1. sed -i '/xx/,$d' file*
复制代码

作者: seesea2517    时间: 2013-06-14 10:36
  1. [seesea@UC ~]$ cat file
  2. abc
  3. bcd
  4. cdf
  5. xx--#--xx
  6. zxc
  7. asd
  8. [seesea@UC ~]$ sed -i '/xx--#--xx/{s/.//g;q}' file
  9. [seesea@UC ~]$ cat file
  10. abc
  11. bcd
  12. cdf
复制代码

作者: seesea2517    时间: 2013-06-14 10:37
写多了
  1. [seesea@UC ~]$ sed -i '/xx--#--xx/{s///;q}' file  
  2. [seesea@UC ~]$ cat file
  3. abc
  4. bcd
  5. cdf
复制代码

作者: 心若寒江雪    时间: 2013-06-14 13:40
sed -i '/xx--#--xx/,$d' file*
作者: kk5234    时间: 2013-06-14 14:34
  1. sed '/xx--#--xx/Q' file
复制代码

作者: wenhq    时间: 2013-06-14 14:52
@kk5234
Q 是啥意思呢?
作者: Shell_HAT    时间: 2013-06-14 15:00
回复 8# wenhq


       Q      Immediately quit the sed script without processing any more input.

作者: wenhq    时间: 2013-06-14 15:08
回复 9# Shell_HAT


    thanks a lot




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