免费注册 查看新帖 |

Chinaunix

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

[文本处理] awk 匹配抽取段平替换指定字符 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-19 21:45 |只看该作者 |倒序浏览
今天工作遇到以下问题还望高手多多指教
  1. <?xml version="1.0"?>
  2. <food>
  3. <name>Belgian Waffles</name>
  4. <price>5</price>
  5. <description>
  6. two of our famous Belgian Waffles with plenty of real maple syrup
  7. </description>
  8. <calories>650</calories>
  9. </food>
  10. ....
  11. ....
  12. ....
  13. </xml>
  14. <?xml version="1.0"?>
  15. <food>
  16. <name>Strawberry Belgian Waffles</name>
  17. <price>7</price>
  18. <description>
  19. light Belgian waffles covered with strawberries and whipped cream
  20. </description>
  21. <calories>900</calories>
  22. </food>
  23. </xml>

  24. 1 抽取calories是900的段也就是抽取如下段
  25. <?xml version="1.0"?>
  26. <food>
  27. <name>Strawberry Belgian Waffles</name>
  28. <price>7</price>
  29. <description>
  30. light Belgian waffles covered with strawberries and whipped cream
  31. </description>
  32. <calories>900</calories>
  33. </food>
  34. </xml>

  35. 2 匹配
  36. price 为5 calories为900时候name替换为GOD

  37. 还望高手多多指教
复制代码

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
2 [报告]
发表于 2012-11-19 22:09 |只看该作者
回复 1# nakalzz
  1. awk '/^<\?xml/{t=$0}{t=t RS $0}/<\/xml/{if(t~"<calories>900<"){print t};t=""}' file


  2.     awk '/^<\?xml/{t=$0}{t=t RS $0}/<\/xml/{if(t~"<calories>900<" && t~"<price>7<"){t=gensub(/(.*<name>)Strawberry Belgian Waffles(<\/name>.*)/,"\\1GOD\\2",1,t);print t};t=""}' file
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-11-19 22:28 |只看该作者
回复 2# yinyuemi


    多谢您的热心回复

第一个我尝试了一下结果中<?xml version="1.0"?>出现两次。。
<?xml version="1.0"?>
<?xml version="1.0"?>
<food>
<name>Strawberry Belgian Waffles</name>
<price>7</price>
<description>
light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
</xml>

第二个name中间的内容不是固定的不知道有什么好的匹配方法

还请多多指教

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
4 [报告]
发表于 2012-11-19 22:35 |只看该作者
回复 3# nakalzz
  1. awk '/^<\?xml/{t=$0}{t=t RS $0}/<\/xml/{if(t~"<calories>900<" && t~"<price>7<"){t=gensub(/(.*<name>)[^<>]+(<\/name>.*)/,"\\1GOD\\2",1,t);print t};t=""}' file
复制代码

论坛徽章:
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
5 [报告]
发表于 2012-11-19 23:32 |只看该作者
本帖最后由 yestreenstars 于 2012-11-20 00:06 编辑

回复 3# nakalzz


    @yinyuemi/^<\?xml/{t=$0}这句改成/^<\?xml/{t=$0;next}

论坛徽章:
0
6 [报告]
发表于 2012-11-19 23:34 |只看该作者
1.
cat file | awk '/<?xml/,/<\/xml>/{ print}' | awk '$0!="</xml>"{t=t$0;next;}{print t$0;t=""}' | grep 900
2.
cat file | awk '/<?xml/,/<\/xml>/{ print}' | awk '$0!="</xml>"{t=t$0;next;}{print t$0;t=""}' | sed 's/\(.*<\)name\(>.*<\/\)name\(><price>5<\/price>.*<calories>900<\/calories>.*\)/\1god\2god\3/g'
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP