免费注册 查看新帖 |

Chinaunix

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

[文本处理] sed匹配问题 [复制链接]

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-09 16:53 |只看该作者 |倒序浏览
  1. sed -r 's/./-&/g'
复制代码
表示在每个字符的前面加一个 -
如果不想在首字符前面加,用[^]该怎么处理呢?
sed 's/[^^.]/_&/g'
这样试了不好用,是不是 [] 内的不支持正则匹配呢?

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
2 [报告]
发表于 2013-01-09 17:10 |只看该作者
试试这个:
  1. sed -r 's/\B/-/g' 1.txt  
复制代码
[] 里的 ^ 应该没有了句首的含义了吧,还真没注意过这个。

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
3 [报告]
发表于 2013-01-09 17:16 |只看该作者
回复 2# seesea2517

嗯,这个可以,多谢


   

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
4 [报告]
发表于 2013-01-09 17:31 |只看该作者
回复 3# asdf2110


    也可以用你那个再加上删除开头的 -: s/^-//g

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
5 [报告]
发表于 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 应该能匹配啊


   

论坛徽章:
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
6 [报告]
发表于 2013-01-09 18:46 |只看该作者
回复 5# asdf2110


    前提是你的文本全部为单词字符。

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
7 [报告]
发表于 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 ~]#
复制代码

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
8 [报告]
发表于 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.
复制代码

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
9 [报告]
发表于 2013-01-10 09:36 |只看该作者
回复 6# blackold

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


   

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
10 [报告]
发表于 2013-01-10 09:36 |只看该作者
啊,版本高的反而不行,我还以为你的版本太低所以不行呢。回复 8# asdf2110


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP