免费注册 查看新帖 |

Chinaunix

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

[文本处理] Sed追加到匹配段的最后一行,及匹配问题,继续请教高手 [复制链接]

论坛徽章:
6
处女座
日期:2014-04-02 16:07:17酉鸡
日期:2014-04-14 10:09:22子鼠
日期:2014-04-17 11:57:30辰龙
日期:2014-09-01 17:14:08戌狗
日期:2014-10-28 12:25:54未羊
日期:2014-11-14 11:31:58
11 [报告]
发表于 2014-04-14 10:39 |只看该作者
回复 9# Iinvincible

明白你的意思了,确实是有问题,话说这样可以么
  1. [root@test sh]# cat o
  2. ###
  3. this is a test file
  4. ###
  5. this is a test file
  6. this is a test file
  7. this is a test file
  8. this is a test file
  9. ###
  10. this is a test file
  11. this is a test file
  12. this is a test file
  13. this is a test file
  14. this is a test file
  15. this is a test file
  16. this is a test file
  17. his is a test file
  18. [root@test sh]#
  19. [root@test sh]# sed -r '1{:1;N;$!b1; s/\n###/\n!!!!!!!!!!!\n###/g;s/$/\n!!!!!!!!!!!/}' o
  20. ###
  21. this is a test file
  22. !!!!!!!!!!!
  23. ###
  24. this is a test file
  25. this is a test file
  26. this is a test file
  27. this is a test file
  28. !!!!!!!!!!!
  29. ###
  30. this is a test file
  31. this is a test file
  32. this is a test file
  33. this is a test file
  34. this is a test file
  35. this is a test file
  36. this is a test file
  37. his is a test file
  38. !!!!!!!!!!!
复制代码

论坛徽章:
0
12 [报告]
发表于 2014-04-14 10:41 |只看该作者
多谢啊。这样可以了

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
13 [报告]
发表于 2014-04-14 10:50 |只看该作者
回复 10# Iinvincible


后面的测试文件明显和1楼的不一样嘛。
  1. sed '1!{/^##/s/.*/111111\n&/};$s/.*/&\n111111/' file
复制代码

论坛徽章:
0
14 [报告]
发表于 2014-04-14 10:54 |只看该作者
回复 13# ly5066113

谢谢啊。格式都一样的。你这个更简洁


   

论坛徽章:
0
15 [报告]
发表于 2014-04-14 11:02 |只看该作者
回复 13# ly5066113

我想把匹配的和追加的都用shell 系统变量,如何写呢?


   

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
16 [报告]
发表于 2014-04-14 11:05 |只看该作者
回复 15# Iinvincible
  1. var=111111
  2. sed "1! {/^##/s/.*/$var\n&/};$ s/.*/&\n$var/" file
复制代码

论坛徽章:
14
15-16赛季CBA联赛之辽宁
日期:2019-06-16 15:47:3515-16赛季CBA联赛之广夏
日期:2016-08-13 21:24:352015亚冠之武里南联
日期:2015-07-07 17:37:372015亚冠之萨济拖拉机
日期:2015-07-06 17:07:482015亚冠之全北现代
日期:2015-06-04 13:54:272015亚冠之城南
日期:2015-05-21 15:43:212015年亚洲杯之伊朗
日期:2015-04-25 18:20:362015年亚洲杯之伊朗
日期:2015-04-20 16:06:052015年亚洲杯之科威特
日期:2015-03-07 12:51:26丑牛
日期:2014-12-30 10:26:38申猴
日期:2014-09-28 22:40:18金牛座
日期:2014-09-13 21:12:22
17 [报告]
发表于 2014-04-14 11:09 |只看该作者
  1. awk 'BEGIN{RS="###";FS='\n'}NF>1{print RS,$0 "!!!!!"}' 1.txt
  2. ###
  3. this is a test file
  4. 111111
  5. !!!!!
  6. ###
  7. this is a test file
  8. this is a test file
  9. this is a test file
  10. this is a test file
  11. !!!!!
  12. ###
  13. this is a test file
  14. this is a test file
  15. this is a test file
  16. this is a test file
  17. this is a test file
  18. this is a test file
  19. this is a test file
  20. his is a test file
  21. !!!!!
复制代码

论坛徽章:
0
18 [报告]
发表于 2014-04-14 11:19 |只看该作者
回复 16# ly5066113


多谢,如果自动保存修改后的文件呢,现在的命令只是输出了结果,如何把结果自动保存到原文件呢

   

论坛徽章:
780
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
19 [报告]
发表于 2014-04-14 11:29 |只看该作者
回复 18# Iinvincible

sed -i ...
   

论坛徽章:
0
20 [报告]
发表于 2014-04-14 14:36 |只看该作者
我现在又遇到了个问题,就是现在不匹配###了

比如要匹配a=[abcd:/efg]

然后sed调用sed '/$a/' ...... 貌似这样不好使啊,还有别的方法吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP