免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
21 [报告]
发表于 2014-04-14 15:29 |只看该作者
继续请教各路高手!

论坛徽章:
769
金牛座
日期: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
22 [报告]
发表于 2014-04-14 17:22 |只看该作者
本帖最后由 Herowinter 于 2014-04-14 17:27 编辑

回复 21# Iinvincible
  1. sep='[abcd:/efg]';var='!!!!!!';sed '/'"$sep"'/{:1;n;$s/.*/&\n'"$var"'/;/^\s*$/!b1;s/.*/'"$var"'&/}' i

  2. [abcd:/efg]
  3. 1111111111
  4. 22222222222
  5. !!!!!!

  6. [abcd:/efg]
  7. 33333333
  8. 5555555555
  9. !!!!!!

复制代码
  1. cat i
  2. [abcd:/efg]
  3. 1111111111
  4. 22222222222


  5. [abcd:/efg]
  6. 33333333
  7. 5555555555
复制代码

论坛徽章:
0
23 [报告]
发表于 2014-04-24 15:13 |只看该作者
回复 22# Herowinter

多谢,貌似这种方式我换了[abcd:/]这样的变量就匹配不上了呀。

[root@root]# sep='[abcd:/efg]';var='!!!!!!';sed '/'"$sep"'/{:1;n;$s/.*/&\n'"$var"'/;/^\s*$/!b1;s/.*/'"$var"'&/}' abc.txt
###
this is a test file
[abcd:/]
this is a test file
this is a test file
this is a test file
this is a test file
###
this is a test file
this is a test file
this is a test file
this is a test file
this is a test file
this is a test file
this is a test file
his is a test file
[abcd:/efg]
1111111111
22222222222
this is a test file
!!!!!!
[root@root]# sep='[abcd:/]';var='dddddd!!!!!!';sed '/'"$sep"'/{:1;n;$s/.*/&\n'"$var"'/;/^\s*$/!b1;s/.*/'"$var"'&/}' abc.txt
###
this is a test file
[abcd:/]
this is a test file
this is a test file
this is a test file
this is a test file
###
this is a test file
this is a test file
this is a test file
this is a test file
this is a test file
this is a test file
this is a test file
his is a test file
[abcd:/efg]
1111111111
22222222222
this is a test file
dddddd!!!!!!

继续请教各路高手,现在不要求必须插到匹配变量这个段的最下面了,只要能匹配上,并且能插到匹配后就可以了。
   

论坛徽章:
769
金牛座
日期: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
24 [报告]
发表于 2014-04-24 16:03 |只看该作者
回复 23# Iinvincible
首先,我之前的写法是有点问题,因为你的分割符
[abcd:/]中 [  /  ]都是元字符,要转义;
还有我当时写的时候,看你的文本段落之间
区分是以空行的,你先在改成####了?


   

论坛徽章:
0
25 [报告]
发表于 2014-04-24 16:06 |只看该作者
本帖最后由 Iinvincible 于 2014-04-24 16:08 编辑

回复 24# Herowinter


恩,原来是###这个,现在不以###或者空行左为段落标记了,都是[adfad:/]或者[adsfadsf:/fdfasdfasfd/adfsf]这种格式做段落标记了。所以我想只要变量传递给sed使用的时候,sed把这个变量作为一个固定的字符串,不进行任何转义并且能工作(精确匹配),就满足需求了。

   

论坛徽章:
769
金牛座
日期: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
26 [报告]
发表于 2014-04-24 16:06 |只看该作者
回复 23# Iinvincible
  1. sep='\[abcd:\/\]';var='dddddd!!!!!!';sed '/'"$sep"'/{:1;n;$s/.*/&\n'"$var"'/;/^\s*$/!b1;s/.*/'"$var"'&/}' i
  2. ###
  3. this is a test file
  4. [abcd:/]
  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. dddddd!!!!!!
  10. ###
  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. this is a test file
  18. his is a test file

  19. [abcd:/efg]
  20. 1111111111
  21. 22222222222
  22. this is a test file
复制代码
  1. ###
  2. this is a test file
  3. [abcd:/]
  4. this is a test file
  5. this is a test file
  6. this is a test file
  7. this is a test file

  8. ###
  9. this is a test file
  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. his is a test file

  17. [abcd:/efg]
  18. 1111111111
  19. 22222222222
  20. this is a test file
复制代码

论坛徽章:
0
27 [报告]
发表于 2014-04-24 16:17 |只看该作者
回复 26# Herowinter

除了用sed还有别的命令可以实现吗?我现在要做的就是弄一个shell变量,然后匹配这个变量,如果匹配上就在下面插入新的变量,你的方法需要用到反斜杠,我不可能每个变量都受到加个反斜杠啊

   

论坛徽章:
30
申猴
日期:2014-04-10 09:43:532015年亚洲杯纪念徽章
日期:2015-03-20 14:40:232015亚冠之阿尔纳斯尔
日期:2015-06-02 18:59:042015亚冠之阿尔希拉尔
日期:2015-06-30 15:22:572015亚冠之大阪钢巴
日期:2015-07-20 10:44:332015亚冠之阿尔纳斯尔
日期:2015-10-28 14:57:5215-16赛季CBA联赛之新疆
日期:2015-12-25 10:18:45黑曼巴
日期:2016-06-26 21:39:5315-16赛季CBA联赛之山西
日期:2016-07-25 21:54:2715-16赛季CBA联赛之北京
日期:2016-10-27 12:07:2315-16赛季CBA联赛之八一
日期:2017-07-07 16:39:0915-16赛季CBA联赛之吉林
日期:2017-09-04 12:14:43
28 [报告]
发表于 2014-04-24 17:30 |只看该作者
本帖最后由 zxy877298415 于 2014-04-24 17:31 编辑

回复 13# ly5066113
根据大师的方法又改了改,不知道有没有不妥的地方。
[root@sinosoft19 ~]# sed -e '1!{/^##/s/.*/111111\n&/}'  -e  '$ a\111111'  a
###
this is a test file
111111
###
this is a test file
dsafsafdsdaf
111111



   

论坛徽章:
769
金牛座
日期: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
29 [报告]
发表于 2014-04-24 20:02 |只看该作者
回复 27# Iinvincible
awk我可以写类似的,用index来判断当前行中是不是含有你想要的分割符,
但这种判断也有一定程度的不严谨性,它不保证当前行仅含有该分隔符。
  1. sep='[abcd:/]';var='!!!!!ddddd';awk -vsep=$sep -vvar=$var 'index($0,sep)>0{k=1} k&&/^[[:space:]]*$/{k=0;print var} END{if(k)print var}1' i

  2. ###
  3. this is a test file
  4. [abcd:/]
  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. !!!!!ddddd

  10. ###
  11. [abcd:/efg]
  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. this is a test file
  18. this is a test file
  19. his is a test file

  20. [abcd:/]
  21. 1111111111
  22. 22222222222
  23. this is a test file
  24. !!!!!ddddd

复制代码
  1. cat i
  2. ###
  3. this is a test file
  4. [abcd:/]
  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. [abcd:/efg]
  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. this is a test file
  18. his is a test file

  19. [abcd:/]
  20. 1111111111
  21. 22222222222
  22. this is a test file
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP