免费注册 查看新帖 |

Chinaunix

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

[文本处理] sed|awk文本格式化 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2022-01-14 16:23 |只看该作者 |倒序浏览
有如下文本2行,需要格式化
301011011|Service Handling Order Exclusiveness Configuration|4050019|4050009|4050008|4050003|4050011|4056015|Configuration of the exclusiveness of service handling orders.|301|301011||0|
301010413|List of IDs of customization process standard events.|1402|1401|1403|1405|List of IDs of customization process standard events. The event IDs in the list are level-3 event IDs and can be separated by commas ( , ), semicolons ( ; ), and '|'.|305|301010||0|

需要替换两个红色“|”之间的“|”为“;”

期待的结果是:
301011011|Service Handling Order Exclusiveness Configuration|4050019;4050009;4050008;4050003;4050011;4056015;Configuration of the exclusiveness of service handling orders.|301|301011||0|
301010413|List of IDs of customization process standard events.|1402;1401;1403;1405;List of IDs of customization process standard events. The event IDs in the list are level-3 event IDs and can be separated by commas ( , ), semicolons ( ; ), and ';'.|305|301010||0|

请大家帮忙。多谢。

论坛徽章:
25
程序设计版块每日发帖之星
日期:2016-05-03 06:20:0015-16赛季CBA联赛之八一
日期:2018-07-05 10:34:09黑曼巴
日期:2018-07-06 15:19:5015-16赛季CBA联赛之佛山
日期:2018-08-03 13:19:3315-16赛季CBA联赛之山西
日期:2018-08-07 19:46:2315-16赛季CBA联赛之广夏
日期:2018-08-08 19:31:5015-16赛季CBA联赛之青岛
日期:2018-11-26 15:21:5015-16赛季CBA联赛之上海
日期:2018-12-11 09:45:3219周年集字徽章-年
日期:2020-04-18 23:54:5215-16赛季CBA联赛之深圳
日期:2020-04-19 21:40:19黑曼巴
日期:2022-04-03 17:55:1315-16赛季CBA联赛之八一
日期:2018-07-03 16:56:46
2 [报告]
发表于 2022-01-14 23:00 |只看该作者
回复 1# lvyuancyx


  1. cat 1
  2. 301011011|Service Handling Order Exclusiveness Configuration|4050019|4050009|4050008|4050003|4050011|4056015|Configuration of the exclusiveness of service handling orders.|301|301011||0|
  3. 301010413|List of IDs of customization process standard events.|1402|1401|1403|1405|List of IDs of customization process standard events. The event IDs in the list are level-3 event IDs and can be separated by commas ( , ), semicolons ( ; ), and '|'.|305|301010||0|

  4. awk '{match($0,"^(([^|]*\\|){2})(.*)(\\|([^|]*\\|){4})$",a);gsub(/\|/,";",a[3]);print a[1]""a[3]""a[4]}' 1
  5. 301011011|Service Handling Order Exclusiveness Configuration|4050019;4050009;4050008;4050003;4050011;4056015;Configuration of the exclusiveness of service handling orders.|301|301011||0|
  6. 301010413|List of IDs of customization process standard events.|1402;1401;1403;1405;List of IDs of customization process standard events. The event IDs in the list are level-3 event IDs and can be separated by commas ( , ), semicolons ( ; ), and ';'.|305|301010||0|
复制代码

论坛徽章:
1
19周年集字徽章-年
日期:2020-10-29 09:39:21
3 [报告]
发表于 2022-01-18 16:51 |只看该作者
[ 本帖最后由 legs 于 2022-01-18 17:09 编辑 ]\n\n没办法编辑了,搞不懂论坛的代码怎么使用的。

perl -pe's#\|(?=.*\|30\d\|)#++$n>2?";":$&#ge;$n=0' file
perl -pe's#(^([^|]+\|){2}|\|30\d\|.*)(*SKIP)(?!)|\|#;#g' file

论坛徽章:
1
19周年集字徽章-年
日期:2020-10-29 09:39:21
4 [报告]
发表于 2022-01-18 17:12 |只看该作者
perl -pe's#(^([^|]+\|){2}|\|30\d\|.*)(*SKIP)(*F)|\|#;#g' file

或者

perl -pe's#\|(?=.*\|30\d\|)#++$n>2?";"&#ge;$n=0' file
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP