免费注册 查看新帖 |

Chinaunix

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

[文本处理] 怎么把多个文件按格式合并并生成json格式? [复制链接]

论坛徽章:
0
41 [报告]
发表于 2013-12-13 09:46 |只看该作者
回复 25# yestreenstars


   我去,昨天说的格式不对,
那个 json格式是这样的,
{
"log1":[
           {"id":"76df34","times":"1000","type":["index","observe"]},
           {"id":"893fed","times":"2000","type":["index","forcast5d","forcast3d"]},
           ...
          ]
"log2":[
           {"ip":"11.11.11.11","times":"20000"},
           {"ip":"22.22.22.22","times":"10000"},
           ...
          ]
}
log1里面表示以id为键值的内容,log2表示ip为键值的,这个好弄不?
中间的空格换行什么的可以不加,只是为了显示清晰。结果为:
{"log1":[{"id":"76df34","times":"1000","type":["index","observe"]},  {"id":"893fed","times":"2000","type":["index","forcast5d","forcast3d"]},  ...]"log2":[{"ip":"11.11.11.11","times":"20000"}, {"ip":"22.22.22.22","times":"10000"},..] }

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
42 [报告]
发表于 2013-12-13 09:52 |只看该作者

论坛徽章:
0
43 [报告]
发表于 2013-12-13 09:54 |只看该作者
回复 42# reyleon


    老大帮帮 忙啊。。

论坛徽章:
0
44 [报告]
发表于 2013-12-13 09:56 |只看该作者
回复 42# reyleon


    你昨天那个程序挺好的,就是我的格式是unix的格式,中间的分隔符是TAB,也就是制表符,完了今天发现json格式不对,能再帮我弄一个嘛。。

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
45 [报告]
发表于 2013-12-13 10:00 |只看该作者
回复 44# yu34po


    还要不要多个文件合并一个文件啊


你发一组数据出来

论坛徽章:
0
46 [报告]
发表于 2013-12-13 10:02 |只看该作者
回复 45# reyleon


   稍等啊,我把最原始的数据发你,原汗原味的unix格式

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
47 [报告]
发表于 2013-12-13 10:03 |只看该作者
回复 46# yu34po


    你加我下面的签名的那个群,然后实时沟通吧,这样太蛋疼了

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
48 [报告]
发表于 2013-12-13 11:58 |只看该作者
  1. #!/bin/bash
  2. cd $(dirname $0)
  3. # Processing Log1
  4. log1 (){
  5.         cat *.log  | grep -E '^[a-z]+:' | sed 's/[ \t]\+//g' | xargs -n3 | \
  6.         awk 'BEGIN{
  7.                 FS="[ :]"
  8.         }
  9.         {       x=$1
  10.                 y=$3
  11.                 z=$5
  12.                 a[$2]+=$4
  13.                 b[$2]=b[$2]?b[$2]","$NF:$NF
  14.         } END {
  15.                 for (i in a){
  16.                         len=split(b[i],t,",")
  17.                         for (j in t){
  18.                                 p["\""t[j]"\""]=0
  19.                         }
  20.                         for (k in p){
  21.                                 v=v?v","k:k
  22.                         }
  23.                         print "{\"" x "\":\"" i "\",\"" y "\":\"" a[i] "\",\"" z "\":[" v "]}"
  24.                         delete t
  25.                         delete p
  26.                         v=""
  27.                 }
  28.         }' | paste -s -d ','
  29. }
  30. # Processing Log2
  31. log2 (){
  32.         cat *.log | grep -E '^([0-9]\.?){1,4}' | \
  33.         awk '{
  34.                 a[$1]+=$2
  35.         } END {
  36.                 for(i in a){
  37.                         print "{\"ip\":" "\"" i "\"" "," "\"times\"" ":\""a[i] "\"}"
  38.                 }
  39.         }' | paste -s -d ','
  40. }
  41. # Print Result
  42. echo -e "{\n\t\"log1\":[$(log1)],\n\t\"log2\":[$(log2)]\n}"
复制代码
效果如下:
  1. [root@centos6-1 txt]# sh json.sh
  2. {
  3.         "log1":[{"id":"e294c2","times":"328790","type":["observe","alarm"]},{"id":"1f7f03","times":"112217","type":["observe","forecast3d","index"]},{"id":"d30b9f","times":"736036","type":["observe","forecast5d","alarm","index"]},{"id":"68faa3","times":"503567","type":["forecast","HBgqxx","forecast3h","HBxzxxz","alarm"]},{"id":"766cbd","times":"6","type":["observe","forecast3d","index"]},{"id":"320723","times":"513787","type":["observe","forecast","alarm","index"]},{"id":"45029b","times":"1284","type":["forecast1d"]},{"id":"16c070","times":"1915","type":["forecast1d"]},{"id":"7c1429","times":"11905555","type":["observe","forecast5d","index"]},{"id":"5aae5b","times":"251","type":["observe","forecast3d","index"]},{"id":"c8d6bf","times":"46623","type":["forecast4d","observe","air","indexen","forecast1d","index"]},{"id":"d5881d","times":"221473","type":["observe","air"]},{"id":"c9f204","times":"8362089","type":["forecast5d","observe"]},{"id":"71f520","times":"3581","type":["air","forecast3d","index"]},{"id":"334cfc","times":"2173","type":["alarm","forecast3d","index"]},{"id":"2389f4","times":"490750","type":["observe","alarm","forecast3d","index"]},{"id":"f63d32","times":"26165611","type":["observe","forecast","forecast3h","temp","all","index"]},{"id":"549d81","times":"3930843","type":["observe","forecast","calendar","alarm","index"]},{"id":"cf2d61","times":"2","type":["observe","index"]},{"id":"3d274c","times":"200007","type":["observe"]},{"id":"674c66","times":"654","type":["observe","forecast","air","alarm","index"]},{"id":"f1c3f8","times":"17","type":["observe"]},{"id":"aa2f1a","times":"2013","type":["forecast5d"]},{"id":"d7d2bc","times":"65348","type":["observe"]},{"id":"580853","times":"1","type":["forecast3d"]},{"id":"d229f3","times":"2","type":["forecast5d"]},{"id":"7d34f2","times":"90","type":["observe","forecast3d","index"]},{"id":"22c100","times":"8","type":["observe","forecast","alarm","index"]},{"id":"e9b455","times":"136251","type":["newsw7","observe","forecast5d","newsl7","alarm","index"]},{"id":"abb2c8","times":"691","type":["forecast3d"]},{"id":"7f9b82","times":"5597","type":["observe","forecast3d","index"]},{"id":"7b0712","times":"1","type":["forecast"]},{"id":"8db47c","times":"4717","type":["forecast","observe"]},{"id":"5b9529","times":"51752","type":["observe","forecast3d","index"]}],
  4.         "log2":[{"ip":"61.4.184.93","times":"11625169"},{"ip":"61.4.184.90","times":"11555629"},{"ip":"61.4.184.81","times":"7487385"},{"ip":"61.4.184.82","times":"34645"},{"ip":"61.4.184.91","times":"11580673"},{"ip":"114.247.188.89","times":"16"},{"ip":"61.4.184.92","times":"11609059"},{"ip":"61.4.184.83","times":"34633"}]
  5. }
  6. [root@centos6-1 txt]#
  7. [root@centos6-1 txt]#

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP