Chinaunix

标题: sed匹配问题 [打印本页]

作者: asdf2110    时间: 2013-01-09 16:53
标题: sed匹配问题
  1. sed -r 's/./-&/g'
复制代码
表示在每个字符的前面加一个 -
如果不想在首字符前面加,用[^]该怎么处理呢?
sed 's/[^^.]/_&/g'
这样试了不好用,是不是 [] 内的不支持正则匹配呢?
作者: seesea2517    时间: 2013-01-09 17:10
试试这个:
  1. sed -r 's/\B/-/g' 1.txt  
复制代码
[] 里的 ^ 应该没有了句首的含义了吧,还真没注意过这个。
作者: asdf2110    时间: 2013-01-09 17:16
回复 2# seesea2517

嗯,这个可以,多谢


   
作者: seesea2517    时间: 2013-01-09 17:31
回复 3# asdf2110


    也可以用你那个再加上删除开头的 -: s/^-//g
作者: asdf2110    时间: 2013-01-09 17:56

回复 4# seesea2517

嗯,那个可以,我又试了awk的gsub和gensub,结果和 sed 有点不一样
  1. echo test | awk '{print gensub(/\B/,"-","g",$0)}'
  2. echo test | awk '{gsub(/\B/,"-",$0);print}'
复制代码
这俩结果都是 : t-e-st

我看网上说 \B 匹配非边界字符,按理说 s 应该能匹配啊


   
作者: blackold    时间: 2013-01-09 18:46
回复 5# asdf2110


    前提是你的文本全部为单词字符。
作者: yestreenstars    时间: 2013-01-09 18:52
回复 5# asdf2110


    版本不同?
  1. [root@localhost ~]# echo test | awk '{print gensub(/\B/,"-","g",$0)}'
  2. t-e-s-t
  3. [root@localhost ~]# echo test | awk '{gsub(/\B/,"-",$0);print}'
  4. t-e-s-t
  5. [root@localhost ~]# awk --version
  6. GNU Awk 3.1.3
  7. Copyright (C) 1989, 1991-2003 Free Software Foundation.

  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.

  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.

  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. [root@localhost ~]#
复制代码

作者: asdf2110    时间: 2013-01-10 09:29
本帖最后由 asdf2110 于 2013-01-10 09:30 编辑

回复 7# yestreenstars
还真是,我的是 3.1.5
  1. $ awk --version
  2. GNU Awk 3.1.5
  3. Copyright (C) 1989, 1991-2005 Free Software Foundation.

  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.

  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.

  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
复制代码

作者: asdf2110    时间: 2013-01-10 09:36
回复 6# blackold

谢谢回复,test 都是单词字符吧,可能真的和版本有关,7L的3.1.3版本正常,我的3.1.5版本不正常


   
作者: yestreenstars    时间: 2013-01-10 09:36
啊,版本高的反而不行,我还以为你的版本太低所以不行呢。回复 8# asdf2110


   
作者: seesea2517    时间: 2013-01-10 10:45
这还真是很奇怪。
作者: asdf2110    时间: 2013-01-10 10:49
回复 10# yestreenstars
  1. [scott@localhost ~]$ echo test | awk '{print gensub(/\B/,"-","g",$0)}'
  2. t-e-s-t
  3. [scott@localhost ~]$ echo test | awk '{gsub(/\B/,"-",$0);print}'
  4. t-e-s-t
  5. [scott@localhost ~]$ awk --version
  6. GNU Awk 3.1.8
  7. 版权所有 © 1989, 1991-2010 自由软件基金会(FSF)。

  8. 该程序为自由软件,你可以在自由软件基金会发布的 GNU 通用公共许可证(GPL)第
  9. 3版或以后版本下修改或重新发布。

  10. 该程序之所以被发布是因为希望他能对你有所用处,但我们不作任何担保。这包含
  11. 但不限于任何商业适售性以及针对特定目的的适用性的担保。详情参见 GNU 通用公
  12. 共许可证(GPL)。

  13. 你应该收到程序附带的一份 GNU 通用公共许可证(GPL)。如果没有收到,请参看 http://www.gnu.org/licenses/ 。
  14. [scott@localhost ~]$
复制代码

作者: yestreenstars    时间: 2013-01-10 10:55
回复 12# asdf2110


    这又是怎么回事呢?
作者: asdf2110    时间: 2013-01-10 10:57
回复 13# yestreenstars
呵呵,具体不知道,现象是
3.1.3 OK
3.1.5 FAIL
3.1.8 OK
   
作者: Shell_HAT    时间: 2013-01-10 10:59
回复 10# yestreenstars


怀疑是3.1.5特有的bug
C:\Test>echo test|gawk_3.1.5.exe "{print gensub(/\B/,\"-\",\"g\",$0)}"
t-e-st

C:\Test>echo test|gawk_3.1.6.exe "{print gensub(/\B/,\"-\",\"g\",$0)}"
t-e-s-t

C:\Test>echo test|gawk_4.0.2.exe "{print gensub(/\B/,\"-\",\"g\",$0)}"
t-e-s-t

作者: yestreenstars    时间: 2013-01-10 11:06
回复 15# Shell_HAT


    了解,多谢版主~




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