在Unix中使用awk或sed进行解析 [英] Parsing using awk or sed in Unix

查看:64
本文介绍了在Unix中使用awk或sed进行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个文件,成千上万的记录具有以下格式:

I have multiple files with hundreds of thousands of records in following format:

2010/08/10 10:07:52|TrainZoom|1393|38797|MyCustomerAPI->,mask = ZPTA,TransId = 1281460071578,testing :-> , capture -> : , IMDB = 9113290830, equipmentName = GMT, technologyName = RMS,,,)|

有些字段用竖线分隔,而在一个字段(最后)内,还有许多其他字段用逗号分隔.我要做的是将所有这些值存储在数据库中.对于上面的示例,我的DB字段将是日期时间,进程名称,线程ID,序列号,API名称,transId,imdb,equipmentName,technologyName.
API名称不是固定格式,但我可以假定其为固定长度.

There are fields separated by pipes and inside a field (last) there are many other fields separated by commas. What I want to do is to store all these values in database. For the above example my DB fields would be date-time, process-name, thread-id, sequence-num, API name, transId, imdb, equipmentName, technologyName.
API Name is not in a fixed format but I can assume that its a fixed length.

请让我知道我是否可以在awk或sed的Unix中做到这一点.

Please let me know if I can do this in Unix using awk or sed.

谢谢

推荐答案

只是为了好玩:

cat file.txt | sed -e 's/\([^|]*\)|\([^|]*\)|\([^|]*\)|\([^|]*\)|/insert into table set date-time=\1, process-name=\2, thread-id=\3, sequence-num=\4, /' -e 's/,[^=]*,/,/g' -e 's/ \([,=]\)/\1/g' -e 's/\([,=]\) /\1/g' -e 's/\([^,]*\)=\([^,]*\),/\1="\2",/g' -e 's/"[^"]*$/";/' | mysql dbname

这篇关于在Unix中使用awk或sed进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆